Question

I created a custom attribute type date for customer in Magento 2 like this:

$code = 'date_custom';
$insertData = array(
        "type"     => "date",
        "backend"  => "",
        "label"    => "Date Custom",
        "input"    => "date",
        "source"   => "",
        "visible"  => true,
        "required" => false,
        "default" => "",
        "frontend" => "",
        "unique"     => false,
        "note"       => ""
      );

$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, $code,  $insertData);
      $attribute   = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, $code);

      $attribute = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, $code);
      $used_in_forms[]="adminhtml_customer";
      $used_in_forms[]="checkout_register";
      $used_in_forms[]="customer_account_create";
      $used_in_forms[]="customer_account_edit";
      $used_in_forms[]="adminhtml_checkout";
      $attribute->setData("used_in_forms", $used_in_forms)
      ->setData("is_used_for_customer_segment", true)
      ->setData("is_system", 0)
      ->setData("is_user_defined", 1)
      ->setData("is_visible", 1)
      ->setData("is_used_in_grid", 1)
      ->setData("is_visible_in_grid", 1)
      ->setData("is_filterable_in_grid", 1)
      ->setData("is_searchable_in_grid", 1);
      ->setData("sort_order", $data['sorting_order']);

    try {
      $attribute->save();
    } catch (Exception $e) {
      $this->_logger->addError($e->getMEssage());
    }

After that i cleared the cache and did reindex, and when reindexing customer grid i got this following error:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'project.customer_entity_date' doesn't exist, query was: SELECT `e`.*, `e`.`entity_id`, TRIM(CONCAT_WS(' ', IF(`e`.`prefix` <> '', `e`.`prefix`, NULL), IF(`e`.`firstname` <> '', `e`.`firstname`, NULL), IF(`e`.`middlename` <> '', `e`.`middlename`, NULL), IF(`e`.`lastname` <> '', `e`.`lastname`, NULL), IF(`e`.`suffix` <> '', `e`.`suffix`, NULL))) AS `name`, `e`.`email`, `e`.`group_id`, `e`.`created_at`, `e`.`website_id`, `e`.`confirmation`, `e`.`created_in`, `e`.`dob`, `e`.`gender`, `e`.`taxvat`, `e`.`lock_expires`, `at_0vudsw3`.`value` AS `0vudsw3`, `at_3m6qzpM`.`value` AS `3m6qzpM`, `at_aMzVP1t`.`value` AS `aMzVP1t`, `at_kCDAzI2`.`value` AS `kCDAzI2`, TRIM(CONCAT_WS(' ', IF(`shipping`.`street` <> '', `shipping`.`street`, NULL), IF(`shipping`.`city` <> '', `shipping`.`city`, NULL), IF(`shipping`.`region` <> '', `shipping`.`region`, NULL), IF(`shipping`.`postcode` <> '', `shipping`.`postcode`, NULL))) AS `shipping_full`, TRIM(CONCAT_WS(' ', IF(`billing`.`street` <> '', `billing`.`street`, NULL), IF(`billing`.`city` <> '', `billing`.`city`, NULL), IF(`billing`.`region` <> '', `billing`.`region`, NULL), IF(`billing`.`postcode` <> '', `billing`.`postcode`, NULL))) AS `billing_full`, `billing`.`firstname` AS `billing_firstname`, `billing`.`lastname` AS `billing_lastname`, `billing`.`telephone` AS `billing_telephone`, `billing`.`postcode` AS `billing_postcode`, `billing`.`country_id` AS `billing_country_id`, `billing`.`region` AS `billing_region`, `billing`.`street` AS `billing_street`, `billing`.`city` AS `billing_city`, `billing`.`fax` AS `billing_fax`, `billing`.`vat_id` AS `billing_vat_id`, `billing`.`company` AS `billing_company` FROM `customer_entity` AS `e`
     LEFT JOIN `customer_entity_int` AS `at_0vudsw3` ON (`at_0vudsw3`.`entity_id` = `e`.`entity_id`) AND (`at_0vudsw3`.`attribute_id` = '236')
     LEFT JOIN `customer_entity_varchar` AS `at_3m6qzpM` ON (`at_3m6qzpM`.`entity_id` = `e`.`entity_id`) AND (`at_3m6qzpM`.`attribute_id` = '235')
     LEFT JOIN `customer_entity_date` AS `at_aMzVP1t` ON (`at_aMzVP1t`.`entity_id` = `e`.`entity_id`) AND (`at_aMzVP1t`.`attribute_id` = '237')
     LEFT JOIN `customer_entity_text` AS `at_kCDAzI2` ON (`at_kCDAzI2`.`entity_id` = `e`.`entity_id`) AND (`at_kCDAzI2`.`attribute_id` = '232')
     LEFT JOIN `customer_address_entity` AS `shipping` ON shipping.entity_id=e.default_shipping
     LEFT JOIN `customer_address_entity` AS `billing` ON billing.entity_id=e.default_billing

when i try to enter add new customer page in admin backend i got the same error as above, and in customer grid there is no customer at all, after that i try to delete this custom attribute, and everything back to normal

Était-ce utile?

La solution

Could you use datetime type instead of date.

It will resolve the issue.

If you want to know more example about datetime type you can refer magento 2 file.

/vendor/magento/module-catalog/Setup/CategorySetup.php
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top