如何使用观察者在 Magento 2 的 customer_entity table 中保存自定义字段值

How can i save custom field value in customer_entity table in Magento 2 using observer

下面是我的观察者代码:

<?php

class CustomerOrderCountObserver implements ObserverInterface
{

    /**
     * @var customerFactory
     */
    private $customerFactory;

    /**
     * 
     * @param CustomerFactory $customerFactory
     */
    public function __construct(
        CustomerFactory $customerFactory
    ) {
          $this->customerFactory = $customerFactory;
    }

    /**
     * Upgrade customer password hash when customer has logged in
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $orderInstance = $observer->getEvent()->getdata();
        $orderIds = $observer->getEvent()->getdata('order_ids');
        $orderCount = is_array($orderIds)?count($orderIds):0;
        $orderId = current($orderIds);
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $session = $objectManager->get('Magento\Customer\Model\Session');

        if($session->isLoggedIn()) {
            $customer = $this->customerFactory->create()->load($session->getCustomerId());
            $orderCount = $orderCount + $customer->getOrderCount();
            $customer->setOrderCount($orderCount);
            $customer->save($customer);
        } 
    }
}

我不知道我做错了什么。它没有保存客户列值 order_count

尝试从模型中加载客户

 $customer = Mage::getModel('customer/customer')->load($session->getCustomerId());

尝试使用 resourceModel 保存客户数据更改,而不是使用模型保存

$customerResourceModel->save($customer);