staempfli / magento2-code-generator

Code generator for Magento 2
250 stars 58 forks source link

Not load attribute from current store #12

Open mato84 opened 6 years ago

mato84 commented 6 years ago

Preconditions

  1. PHP 7.0
  2. Magento 2.2.4

Steps to reproduce mg2-codegen template:generate crudEAV magento/admin->content->gridItem->item->edit If have more than one store and form item have input select. There is a problem: I select any value from any input select field for the US store view and save it. Then I switch to other store view (for example UK store) and select the same value for the same input select field and save it, this value is not saved. The problem lies in $objectInstance->load($data['entity_id']); method, that loads object with attributes which belong to one store view, regardless the store view this object is saved form.

In order to fix this issue the following should be done: in resource model change method protected function _getLoadAttributesSelect($object, $table)

from that

protected function _getLoadAttributesSelect($object, $table)
    {
        if ($this->_storeManager->hasSingleStore()) {
            $storeId = (int) $this->_storeManager->getStore(true)->getId();
        } else {
            $storeId = (int) $object->getStoreId();
        }

        $storeIds = [$this->getDefaultStoreId()];
        if ($storeId != $this->getDefaultStoreId()) {
            $storeIds[] = $storeId;
        }
        $select = $this->getConnection()->select()->from(
            $table,
            []
        )->where(
            $this->getEntityIdField() . ' =?',
            $object->getId()
        )->where('store_id IN (?)', $storeIds);
        return $select;
    }
jalogut commented 6 years ago

Hi @mato84

Thank you for reporting this issue and providing a solution. I leave your comment here, so other people facing same problem, can fix it. This method is a magento core method, so we can expect that to be fixed in future releases. If not, I will add the fix within the code templates in next release.