2

i created 2 columns table. although entity_id is primary key but not auto_increment. entity_id is foreign key of quote table.

db_schema.xml

<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">                                        
    <table name="proxy_quote">
        <column xsi:type="int" name="entity_id" nullable="false" padding="10" unsigned="true" comment="quote_entity_id"/>
        <column xsi:type="int" name="admin_user_id" nullable="false" comment="proxy_quote_user_id"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="entity_id"/>
        </constraint>
    </table>
</schema>

Model structure is like this

Model/
├── ProxyQuote.php
└── ResourceModel
    ├── ProxyQuote
    │   └── Collection.php
    └── ProxyQuote.php

Collection.php

lass Collection extends AbstractCollection                                                                                                                                                         
{
    protected $_idFieldName = 'entity_id';

    protected function _construct()
    {
        $this->_init(
            'My\Module\Model\ProxyQuote',
            'My\Module\Model\ResourceModel\ProxyQuote'
        );
    }
}

ResourceModel\ProxyQuote.php

class ProxyQuote extends AbstractDb                                                                                                                                                                 
{
    protected $_idFieldName = 'entity_id';

    protected function _construct()
    {
        $this->_init('proxy_quote', $this->_idFieldName);
    }
}

Model/ProxyQuote.php

class ProxyQuote extends AbstractModel                                                                                                                                                              
{
    /**
     * cache tag
     */
    const CACHE_TAG = 'proxy_quote';

    /**
     * @var string
     */
    protected $_cacheTag = 'proxy_quote';

    /**
     * @var string
     */
    protected $_eventPrefix = 'proxy_quote';

    protected function _construct()
    {
        $this->_init(
            'My\Module\Model\ResourceModel\ProxyQuote'
        );
    }

    /**
     * @return mixed
     */
    public function getEntityId()
    {
        return $this->getData('entity_id');
    }

    /**
     * @param int $entityId
     * @return Quote
     */
    public function setEntityId($entityId)
    {
        return $this->setData('entity_id', $entityId);
    }

    /**
     * @return mixed
     */
    public function getAdminUserId()
    {
        return $this->getData('admin_user_id');
    }

    /**
     * @param int $adminUserId
     * @return Quote
     */
    public function setAdminUserId($adminUserId)
    {
        return $this->setData('admin_user_id', $adminUserId);
    }
}

Observer

    public function execute(\Magento\Framework\Event\Observer $observer)                                                                                                                            
    {
        $adminUserId = $this->session->getLoggedAsCustomerAdmindId();
        $quoteId = $observer->getEvent()->getQuote()->getId();
        $proxyQuote = $this->proxyQuoteFactory->create();

        $proxyQuote->setEntityId($quoteId);
        $proxyQuote->setAdminUserId($adminUserId);
        $proxyQuote->save();

        $this->logger->debug(sprintf('ProxyQuote, quote_id:%d, admin_user_id:%d', $quoteId, $adminUserId));
    }

i could get entity_id, admin_user_id(from debug log). also i could get $proxyQuote->getAdminUserId() after $proxyQuote->setAdminUserId($adminUserId);

but nothing is saved. no record.

i wonder the entity_id is primay but i have to set entity_id because it is not auto_increment column. does it make something wrong?

1

To be able to save a model without a primary key that is auto incremented you will need to set the flag _isPkAutoIncrement in your resource model:

class ProxyQuote extends AbstractDb
{
    protected $_idFieldName = 'entity_id';

    protected function _construct()
    {
        $this->_init('proxy_quote', $this->_idFieldName);
        $this->_isPkAutoIncrement = false;
    }
}

Otherwise Magento thinks that the resource already exist in the database as the id is set and tries to update it, it will then fail to make an update as there is no entry.

In AbstractDb there is a function called updateObject. Here you can see how the isPkAutoIncrement works in action when set.

\Magento\Framework\Model\ResourceModel\Db\AbstractDb::updateObject
1
  • I really puzzled about this problem. thanks it solved.
    – grandan
    Oct 18 '20 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.