150 \
Magento\Framework\Event\ManagerInterface $eventManager,
158 $this->_eventManager = $eventManager;
161 $this->_eavEntityFactory = $eavEntityFactory;
162 $this->_resourceHelper = $resourceHelper;
163 $this->_universalFactory = $universalFactory;
200 $this->_staticFields[$field] = $field;
248 $this->_entity = $this->_eavEntityFactory->create()->setType(
$entity);
251 __(
'The "%1" entity supplied is invalid. Verify the entity and try again.', print_r(
$entity, 1))
265 if (empty($this->_entity)) {
290 if (is_object($object)) {
307 if (!$object instanceof $this->_itemObjectClass) {
309 __(
"The object wasn't added because it's invalid. To continue, enter a valid object and try again.")
312 return parent::addItem($object);
327 return $this->
getEntity()->getAttribute($attributeCode);
357 if (!$attributeModel) {
358 throw new \Magento\Framework\Exception\LocalizedException(
359 __(
'Invalid attribute identifier for filter (%1)', get_class(
$attribute))
362 $attribute = $attributeModel->getAttributeCode();
372 $conditionSql =
'(' . implode(
') OR (', $sqlArr) .
')';
374 if ($condition ===
null) {
380 if (!empty($conditionSql)) {
382 $this->invalidateSize();
384 throw new \Magento\Framework\Exception\LocalizedException(
385 __(
'Invalid attribute identifier for filter (%1)', get_class(
$attribute))
418 if (isset($this->_staticFields[
$attribute])) {
419 $this->
getSelect()->order(
"e.{$attribute} {$dir}");
422 if (isset($this->_joinAttributes[
$attribute])) {
423 $attrInstance = $this->_joinAttributes[
$attribute][
'attribute'];
426 $attrInstance = $this->
getEntity()->getAttribute($attribute);
431 if ($attrInstance->getBackend()->isStatic()) {
432 $orderExpr = $entityField;
443 $orderExpr = new \Zend_Db_Expr(
"CAST({$this->_prepareOrderExpression($orderExpr)} AS SIGNED)");
446 $orderExpr .=
' ' . $dir;
461 if ($columnEntry[2] != $field) {
465 return $columnEntry[1];
495 $this->_selectAttributes[$attrCode] =
$attr->getId();
498 if (isset($this->_joinAttributes[
$attribute])) {
499 $attrInstance = $this->_joinAttributes[
$attribute][
'attribute'];
501 $attrInstance = $this->_eavConfig->getAttribute($this->
getEntity()->
getType(), $attribute);
503 if (empty($attrInstance)) {
506 'The "%1" attribute requested is invalid. Verify the attribute and try again.',
511 $this->_selectAttributes[$attrInstance->getAttributeCode()] = $attrInstance->getId();
538 if (!isset($this->_staticFields[$field])) {
539 $this->_staticFields[$field] = $field;
561 if (isset($this->_joinFields[
$alias])) {
562 throw new LocalizedException(
__(
'Joint field or attribute expression with this alias is already declared'));
568 $fullExpression = $expression;
571 if (isset($this->_staticFields[$attributeItem])) {
572 $attrField = sprintf(
'e.%s', $attributeItem);
574 $attributeInstance = $this->
getAttribute($attributeItem);
576 if ($attributeInstance->getBackend()->isStatic()) {
577 $attrField =
'e.' . $attributeItem;
584 $fullExpression = str_replace(
'{{attribute}}', $attrField, $fullExpression);
585 $fullExpression = str_replace(
'{{' . $attributeItem .
'}}', $attrField, $fullExpression);
588 $this->
getSelect()->columns([$alias => $fullExpression]);
590 $this->_joinFields[
$alias] = [
'table' =>
false,
'field' => $fullExpression];
613 if (isset($this->_staticFields[
$attribute])) {
618 if (isset($this->_joinAttributes[
$attribute])) {
619 $attrInstance = $this->_joinAttributes[
$attribute][
'attribute'];
622 $attrInstance = $this->
getEntity()->getAttribute($attribute);
626 if ($attrInstance->getBackend()->isStatic()) {
665 if (isset($this->_joinAttributes[
$alias])) {
669 $bindAttribute =
null;
671 if (is_string($bind)) {
675 if (!$bindAttribute || !$bindAttribute->isStatic() && !$bindAttribute->getId()) {
676 throw new LocalizedException(
__(
'The foreign key is invalid. Verify the foreign key and try again.'));
682 if (isset($attrArr[1])) {
693 if (isset($this->_joinEntities[
$entity])) {
696 $entity = $this->_eavEntityFactory->create()->setType($attrArr[0]);
700 throw new LocalizedException(
__(
'The entity type is invalid. Verify the entity type and try again.'));
704 if (!isset($this->_joinEntities[
$entity->getType()])) {
713 throw new LocalizedException(
__(
'The attribute type is invalid. Verify the attribute type and try again.'));
716 if (empty($filter)) {
717 $filter =
$entity->getLinkField();
721 $this->_joinAttributes[
$alias] = [
723 'bindAttribute' => $bindAttribute,
753 if (isset($this->_joinFields[
$alias])) {
761 list($pKey, $fKey) = explode(
'=', $bind);
762 $pKey = $this->
getSelect()->getConnection()->quoteColumnAs(trim($pKey),
null);
768 $joinMethod =
'joinLeft';
771 $joinMethod =
'join';
774 $condArr = [$bindCond];
777 if ($cond !==
null) {
778 if (is_array($cond)) {
779 foreach ($cond as $key =>
$value) {
783 $condArr[] = str_replace(
'{{table}}', $tableAlias, $cond);
786 $cond =
'(' . implode(
') AND (', $condArr) .
')';
792 $field ? [
$alias => $field] : []
796 $this->_joinFields[
$alias] = [
'table' => $tableAlias,
'field' => $field];
823 if (empty($tableAlias)) {
832 if (isset($this->_joinFields[
$alias])) {
835 $this->_joinFields[
$alias] = [
'table' => $tableAlias,
'field' => $field];
839 list($pKey, $fKey) = explode(
'=', $bind);
840 $bindCond = $tableAlias .
'.' . $pKey .
'=' . $this->_getAttributeFieldName($fKey);
845 $joinMethod =
'joinLeft';
849 $joinMethod =
'join';
851 $condArr = [$bindCond];
854 if ($cond !==
null) {
855 if (is_array($cond)) {
856 foreach ($cond as $key =>
$value) {
857 $condArr[] = $this->_getConditionSql($tableAlias .
'.' . $key,
$value);
860 $condArr[] = str_replace(
'{{table}}', $tableAlias, $cond);
863 $cond =
'(' . implode(
') AND (', $condArr) .
')';
880 $this->_selectAttributes = [];
897 $this->setCurPage($pageNum)->setPageSize($pageSize);
908 public function load($printQuery =
false, $logQuery =
false)
910 if ($this->isLoaded()) {
913 \Magento\Framework\Profiler::start(
'EAV:load_collection');
915 \Magento\Framework\Profiler::start(
'before_load');
916 $this->_eventManager->dispatch(
'eav_collection_abstract_load_before', [
'collection' => $this]);
917 $this->_beforeLoad();
918 \Magento\Framework\Profiler::stop(
'before_load');
920 $this->_renderFilters();
921 $this->_renderOrders();
923 \Magento\Framework\Profiler::start(
'load_entities');
924 $this->_loadEntities($printQuery, $logQuery);
925 \Magento\Framework\Profiler::stop(
'load_entities');
926 \Magento\Framework\Profiler::start(
'load_attributes');
927 $this->_loadAttributes($printQuery, $logQuery);
928 \Magento\Framework\Profiler::stop(
'load_attributes');
930 \Magento\Framework\Profiler::start(
'set_orig_data');
931 foreach ($this->_items as
$item) {
932 $item->setOrigData();
933 $this->beforeAddLoadedItem(
$item);
934 $item->setDataChanges(
false);
936 \Magento\Framework\Profiler::stop(
'set_orig_data');
938 $this->_setIsLoaded();
939 \Magento\Framework\Profiler::start(
'after_load');
941 \Magento\Framework\Profiler::stop(
'after_load');
943 \Magento\Framework\Profiler::stop(
'EAV:load_collection');
956 $idsSelect = clone $this->getSelect();
957 $idsSelect->reset(\
Magento\Framework\DB\Select::ORDER);
958 $idsSelect->reset(\
Magento\Framework\DB\Select::LIMIT_COUNT);
959 $idsSelect->reset(\
Magento\Framework\DB\Select::LIMIT_OFFSET);
960 $idsSelect->reset(\
Magento\Framework\DB\Select::COLUMNS);
961 $idsSelect->columns(
'e.' . $this->getEntity()->getIdFieldName());
962 $idsSelect->limit($limit, $offset);
974 public function getAllIds($limit =
null, $offset =
null)
976 return $this->getConnection()->fetchCol($this->_getAllIdsSelect($limit, $offset), $this->_bindParams);
986 $idsSelect = clone $this->getSelect();
987 $idsSelect->reset(\
Magento\Framework\DB\Select::ORDER);
988 $idsSelect->reset(\
Magento\Framework\DB\Select::LIMIT_COUNT);
989 $idsSelect->reset(\
Magento\Framework\DB\Select::LIMIT_OFFSET);
990 $idsSelect->reset(\
Magento\Framework\DB\Select::COLUMNS);
991 $idsSelect->reset(\
Magento\Framework\DB\Select::GROUP);
992 $idsSelect->columns(
'e.' . $this->getEntity()->getIdFieldName());
1006 foreach ($this->getItems() as
$item) {
1019 public function delete()
1021 foreach ($this->getItems() as $key =>
$item) {
1022 $this->getEntity()->delete(
$item);
1023 unset($this->_items[$key]);
1038 $entityIdField = $this->getEntity()->getLinkField();
1039 foreach ($arr as
$row) {
1040 $entityId =
$row[$entityIdField];
1041 if (!isset($this->_items[$entityId])) {
1042 $this->_items[$entityId] = $this->getNewEmptyItem();
1043 $this->_items[$entityId]->setData(
$row);
1045 $this->_items[$entityId]->addData(
$row);
1059 $entityIdField = $this->getEntity()->getLinkField();
1060 foreach ($this->getItems() as
$item) {
1074 return $this->getIdFieldName();
1084 if ($this->_idFieldName ===
null) {
1085 $this->_setIdFieldName($this->getEntity()->getIdFieldName());
1088 return $this->_idFieldName;
1099 return $this->_setIdFieldName($fieldName);
1110 public function _loadEntities($printQuery =
false, $logQuery =
false)
1114 if ($this->_pageSize) {
1115 $this->getSelect()->limitPage($this->getCurPage(), $this->_pageSize);
1118 $this->printLogQuery($printQuery, $logQuery);
1125 $query = $this->getSelect();
1126 $rows = $this->_fetchAll(
$query);
1127 }
catch (\Exception $e) {
1128 $this->printLogQuery(
false,
true,
$query);
1132 foreach ($rows as
$value) {
1133 $object = $this->getNewEmptyItem()->setData(
$value);
1134 $this->addItem($object);
1135 if (isset($this->_itemsById[$object->getId()])) {
1136 $this->_itemsById[$object->getId()][] = $object;
1138 $this->_itemsById[$object->getId()] = [$object];
1159 if (empty($this->_items) || empty($this->_itemsById) || empty($this->_selectAttributes)) {
1165 $tableAttributes = [];
1166 $attributeTypes = [];
1167 foreach ($this->_selectAttributes as
$attributeCode => $attributeId) {
1168 if (!$attributeId) {
1173 $tableAttributes[
$attribute->getBackendTable()][] = $attributeId;
1174 if (!isset($attributeTypes[
$attribute->getBackendTable()])) {
1183 $selects[$attributeTypes[
$table]][] = $this->_addLoadAttributesSelectValues(
1189 $selectGroups = $this->_resourceHelper->getLoadAttributesSelectGroups($selects);
1190 foreach ($selectGroups as $selects) {
1191 if (!empty($selects)) {
1193 if (is_array($selects)) {
1194 $select = implode(
' UNION ALL ', $selects);
1199 }
catch (\Exception $e) {
1200 $this->printLogQuery(
true,
true,
$select);
1205 $this->_setItemAttributeValue(
$value);
1222 if (empty($attributeIds)) {
1223 $attributeIds = $this->_selectAttributes;
1226 $linkField =
$entity->getLinkField();
1227 $select = $this->getConnection()->select()
1229 [
'e' => $this->getEntity()->getEntityTable()],
1234 "e.{$linkField} = t_d.{$linkField}",
1235 [
't_d.attribute_id']
1237 " e.entity_id IN (?)",
1238 array_keys($this->_itemsById)
1240 't_d.attribute_id IN (?)',
1246 'entity_type_id =?',
1265 $select->columns([
'value' =>
't_d.value']);
1280 $entityIdField = $this->getEntity()->getEntityIdField();
1281 $entityId = $valueInfo[$entityIdField];
1282 if (!isset($this->_itemsById[$entityId])) {
1284 __(
'A header row is missing for an attribute. Verify the header row and try again.')
1287 $attributeCode = array_search($valueInfo[
'attribute_id'], $this->_selectAttributes);
1289 $attribute = $this->_eavConfig->getAttribute(
1290 $this->getEntity()->getType(),
1291 $valueInfo[
'attribute_id']
1296 foreach ($this->_itemsById[$entityId] as $object) {
1311 return $this->getConnection()->getTableName(self::ATTRIBUTE_TABLE_ALIAS_PREFIX .
$attributeCode);
1324 if (isset($this->_joinAttributes[
$attributeCode][
'condition_alias'])) {
1325 return $this->_joinAttributes[
$attributeCode][
'condition_alias'];
1338 __(
'The "%1" attribute name is invalid. Reset the name and try again.',
$attributeCode)
1349 $fieldName = $this->_getAttributeTableAlias(
$attributeCode) .
'.value';
1376 $fkAttribute = $this->_joinAttributes[
$attributeCode][
'bindAttribute'];
1377 $fkTable = $this->_getAttributeTableAlias($fkName);
1379 if ($fkAttribute->getBackend()->isStatic()) {
1380 if (isset($this->_joinAttributes[$fkName])) {
1381 $fKey = $fkTable .
'.' . $fkAttribute->getAttributeCode();
1383 $fKey =
'e.' . $fkAttribute->getAttributeCode();
1386 $this->_addAttributeJoin($fkAttribute->getAttributeCode(), $joinType);
1387 $fKey = $fkTable .
'.value';
1389 $pKey = $attrTable .
'.' . $this->_joinAttributes[
$attributeCode][
'filter'];
1392 $fKey =
'e.' . $this->getEntityPkName(
$entity);
1393 $pKey = $attrTable .
'.' . $this->getEntityPkName(
$entity);
1399 __(
'The "%1" attribute name is invalid. Reset the name and try again.',
$attributeCode)
1404 $attrFieldName = $attrTable .
'.' .
$attribute->getAttributeCode();
1406 $attrFieldName = $attrTable .
'.value';
1412 $condArr = [
"{$pKey} = {$fKey}"];
1414 $condArr[] = $this->getConnection()->quoteInto(
1415 $connection->quoteColumnAs(
"{$attrTable}.attribute_id",
null) .
' = ?',
1423 $joinMethod = $joinType ==
'left' ?
'joinLeft' :
'join';
1433 $this->_joinFields[
$attributeCode] = [
'table' =>
'',
'field' => $attrFieldName];
1447 return $entity->getEntityIdField();
1463 $this->getSelect()->{
$method}(
1464 [$tableAlias =>
$attribute->getBackend()->getTable()],
1465 '(' . implode(
') AND (', $condition) .
')',
1466 [$fieldCode => $fieldAlias]
1484 return $this->_getConditionSql($this->_getAttributeFieldName(
$attribute), $condition);
1486 if (isset($this->_staticFields[
$attribute])) {
1487 return $this->_getConditionSql($this->getConnection()->quoteIdentifier(
'e.' .
$attribute), $condition);
1490 if (isset($this->_joinAttributes[
$attribute])) {
1497 $conditionSql = $this->_getConditionSql(
1498 $this->getConnection()->quoteIdentifier(
'e.' .
$attribute),
1502 if (isset($condition[
'null'])) {
1506 $this->_addAttributeJoin(
$attribute, $joinType);
1507 if (isset($this->_joinAttributes[
$attribute][
'condition_alias'])) {
1508 $field = $this->_joinAttributes[
$attribute][
'condition_alias'];
1510 $field = $this->_getAttributeTableAlias(
$attribute) .
'.value';
1512 $conditionSql = $this->_getConditionSql($field, $condition);
1515 return $conditionSql;
1531 parent::setOrder(
$attr, $dir);
1547 foreach ($this->getItems() as $key =>
$item) {
1548 $arr[$key] =
$item->toArray($arrAttributes);
1560 if (!$this->_isOrdersRendered) {
1561 foreach ($this->_orders as
$attribute => $direction) {
1562 $this->addAttributeToSort(
$attribute, $direction);
1564 $this->_isOrdersRendered =
true;
1589 $this->_selectEntityTypes = [];
1590 $this->_selectAttributes = [];
1591 $this->_filterAttributes = [];
1592 $this->_joinEntities = [];
1593 $this->_joinAttributes = [];
1594 $this->_joinFields = [];
1618 return array_keys($this->_items);
1628 $this->_itemsById = [];
1629 return parent::clear();
1639 $this->_itemsById = [];
1640 return parent::removeAllItems();
1651 if (isset($this->_items[$key])) {
1652 unset($this->_itemsById[$this->_items[$key]->getId()]);
1654 return parent::removeItemByKey($key);
1666 return $this->getSelect()->getPart(Select::FROM)[
'e'][
'tableName'];
1681 return $this->addAttributeToSelect($field);
1693 return $this->removeAttributeToSelect($field);
1704 return $this->removeAttributeToSelect();
1714 private function invalidateSize(): void
1716 $this->_totalRecords =
null;
addFieldToFilter($attribute, $condition=null)
_init($model, $entityModel)
getAttribute($attributeCode)
removeAttributeToSelect($attribute=null)
groupByAttribute($attribute)
elseif(isset( $params[ 'redirect_parent']))
_prepareOrderExpression($field)
load($printQuery=false, $logQuery=false)
_addAttributeJoin($attributeCode, $joinType='inner')
setOrder($attribute, $dir=self::SORT_ORDER_ASC)
toArray($arrAttributes=[])
addAttributeToSelect($attribute, $joinType=false)
_loadAttributes($printQuery=false, $logQuery=false)
removeFieldFromSelect($field)
joinField($alias, $table, $field, $bind, $cond=null, $joinType='inner')
removeAllFieldsFromSelect()
_setItemAttributeValue($valueInfo)
joinAttribute($alias, $attribute, $bind, $filter=null, $joinType='inner', $storeId=null)
getEntityPkName(\Magento\Eav\Model\Entity\AbstractEntity $entity)
__construct(\Magento\Framework\Data\Collection\EntityFactory $entityFactory, \Psr\Log\LoggerInterface $logger, \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Eav\Model\Config $eavConfig, \Magento\Framework\App\ResourceConnection $resource, \Magento\Eav\Model\EntityFactory $eavEntityFactory, \Magento\Eav\Model\ResourceModel\Helper $resourceHelper, \Magento\Framework\Validator\UniversalFactory $universalFactory, \Magento\Framework\DB\Adapter\AdapterInterface $connection=null)
setConnection(\Magento\Framework\DB\Adapter\AdapterInterface $conn)
_getAttributeConditionSql($attribute, $condition, $joinType='inner')
joinTable($table, $bind, $fields=null, $cond=null, $joinType='inner')
_getAllIdsSelect($limit=null, $offset=null)
isAttributeAdded($attributeCode)
_getConditionSql($fieldName, $condition)
setPage($pageNum, $pageSize)
_joinAttributeToSelect($method, $attribute, $tableAlias, $condition, $fieldCode, $fieldAlias)
setRowIdFieldName($fieldName)
getAllIds($limit=null, $offset=null)
_getAttributeFieldName($attributeCode)
addAttributeToSort($attribute, $dir=self::SORT_ORDER_ASC)
addItem(\Magento\Framework\DataObject $object)
const ATTRIBUTE_TABLE_ALIAS_PREFIX
_getAttributeTableAlias($attributeCode)
addExpressionAttributeToSelect($alias, $expression, $attribute)
_addLoadAttributesSelectValues($select, $table, $type)
addAttributeToFilter($attribute, $condition=null, $joinType='inner')
addEntityTypeToSelect($entityType, $prefix)
_getLoadAttributesSelect($table, $attributeIds=[])
const DEFAULT_ENTITY_TABLE
addFieldToSelect($field, $alias=null)
setItemObjectClass($className)