Esempio n. 1
0
 /**
  * Load model attributes data
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return $this
  */
 protected function _loadModelAttributes($object)
 {
     if (!$object->getId()) {
         return $this;
     }
     \Magento\Framework\Profiler::start('load_model_attributes');
     $selects = array();
     foreach (array_keys($this->getAttributesByTable()) as $table) {
         $attribute = current($this->_attributesByTable[$table]);
         $eavType = $attribute->getBackendType();
         $select = $this->_getLoadAttributesSelect($object, $table);
         $selects[$eavType][] = $select->columns('*');
     }
     $selectGroups = $this->_resourceHelper->getLoadAttributesSelectGroups($selects);
     foreach ($selectGroups as $selects) {
         if (!empty($selects)) {
             $select = $this->_prepareLoadSelect($selects);
             $values = $this->_getReadAdapter()->fetchAll($select);
             foreach ($values as $valueRow) {
                 $this->_setAttributeValue($object, $valueRow);
             }
         }
     }
     \Magento\Framework\Profiler::stop('load_model_attributes');
     return $this;
 }
 /**
  * Load attributes into loaded entities
  *
  * @param bool $printQuery
  * @param bool $logQuery
  * @return $this
  * @throws EavException
  * @throws \Exception
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function _loadAttributes($printQuery = false, $logQuery = false)
 {
     if (empty($this->_items) || empty($this->_itemsById) || empty($this->_selectAttributes)) {
         return $this;
     }
     $entity = $this->getEntity();
     $tableAttributes = [];
     $attributeTypes = [];
     foreach ($this->_selectAttributes as $attributeCode => $attributeId) {
         if (!$attributeId) {
             continue;
         }
         $attribute = $this->_eavConfig->getAttribute($entity->getType(), $attributeCode);
         if ($attribute && !$attribute->isStatic()) {
             $tableAttributes[$attribute->getBackendTable()][] = $attributeId;
             if (!isset($attributeTypes[$attribute->getBackendTable()])) {
                 $attributeTypes[$attribute->getBackendTable()] = $attribute->getBackendType();
             }
         }
     }
     $selects = [];
     foreach ($tableAttributes as $table => $attributes) {
         $select = $this->_getLoadAttributesSelect($table, $attributes);
         $selects[$attributeTypes[$table]][] = $this->_addLoadAttributesSelectValues($select, $table, $attributeTypes[$table]);
     }
     $selectGroups = $this->_resourceHelper->getLoadAttributesSelectGroups($selects);
     foreach ($selectGroups as $selects) {
         if (!empty($selects)) {
             try {
                 $select = implode(' UNION ALL ', $selects);
                 $values = $this->getConnection()->fetchAll($select);
             } catch (\Exception $e) {
                 $this->printLogQuery(true, true, $select);
                 throw $e;
             }
             foreach ($values as $value) {
                 $this->_setItemAttributeValue($value);
             }
         }
     }
     return $this;
 }