我想在magento2的客戶表中添加自定義列(電話),並希望在客戶註冊時在此字段中添加值。如何在Magento2的客戶表中添加自定義列?
對於這第一個我在customer_entity table.While創建DB列(電話)在的Magento /客戶/控制器/帳號/ CreatePost創造客戶,當我調用$客戶 - > setTelephone( '1234567890')。 php中執行函數。它給出了一個錯誤在Magento/Customer/Model/Data/Customer.php中未定義函數setTelephone。但是我已經在這個模型中創建了這個函數。
的Magento /客戶/控制器/帳戶/ CreatePost.php
public function execute()
{
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
if ($this->session->isLoggedIn() || !$this->registration->isAllowed()) {
$resultRedirect->setPath('*/*/');
return $resultRedirect;
}
if (!$this->getRequest()->isPost()) {
$url = $this->urlModel->getUrl('*/*/create', ['_secure' => true]);
$resultRedirect->setUrl($this->_redirect->error($url));
return $resultRedirect;
}
$this->session->regenerateId();
try {
$address = $this->extractAddress();
$addresses = $address === null ? [] : [$address];
$customer = $this->customerExtractor->extract('customer_account_create', $this->_request);
$customer->setAddresses($addresses);
//Here is I set the telephone and it is giving an error
$customer->setTelephone('1234567890');
的Magento /客戶/型號/數據/ Customer.php
public function setTelephone($telephone)
{
return $this->setData(self::TELEPHONE, $telephone);
}
public function getTelephone()
{
return $this->_get(self::TELEPHONE);
}
的Magento /客戶/阿比/Data/CustomerInterface.php
<?php
namespace Magento\Customer\Api\Data;
interface CustomerInterface extends \Magento\Framework\Api\CustomAttributesDataInterface
{
/* Add this code*/
const TELEPHONE = 'telephone';
public function getTelephone();
public function setTelephone($telephone);
}
試圖做自定義模塊,但它給出了一個錯誤。
1 exception(s): Exception #0 (Magento\Framework\Exception\LocalizedException): Please upgrade your database: Run "bin/magento setup:upgrade" from the Magento root directory. The following modules are outdated: Onjection_Customer data: current version - none, required version - 1.0.0...
模塊代碼
應用程序/代碼/ Onjection /客戶/和registration.php
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Onjection_Customer', __DIR__ );
2.app/code/Onjection/Customer /etc/module.xml
3.app/code/Onjection/Customer/Setup/InstallData.php
<?php
namespace Onjection\Customer\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
/**
* @codeCoverageIgnore
*/
class InstallData implements InstallDataInterface
{
protected $customerSetupFactory;
/**
* @var AttributeSetFactory
*/
private $attributeSetFactory;
/**
* @param CustomerSetupFactory $customerSetupFactory
* @param AttributeSetFactory $attributeSetFactory
*/
public function __construct(
CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
/** @var $attributeSet AttributeSet */
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$customerSetup->addAttribute(Customer::ENTITY, 'mobile', [
'type' => 'varchar',
'label' => 'Mobile',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'mobile')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['customer_address_edit'],
]);
$attribute->save();
$setup->endSetup();
}
}
命令用於安裝:
PHP斌/ Magento的設置:升級
php bin/magento setup:static-content:deploy
這不是使用Magento的正確方法。見[這裏](https://magento.stackexchange.com/questions/88245/magento2-create-a-customer-custom-attribute)。你不應該修改核心文件。 – subroutines
@subroutines如何在客戶註冊頁面添加這個自定義屬性?他們的任何方法都可以在不創建模塊的情況下執行此操作? – Tomas
'telephone'已經是一個現有的客戶地址屬性,您可以查看定製'magento \ module-customer \ view \ frontend \ templates \ form \ register.phtml'以在註冊頁面上顯示它。 – subroutines