Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Collection.php
Go to the documentation of this file.
1 <?php
7 
14 class Collection extends \Magento\Eav\Model\Entity\Collection\VersionControl\AbstractCollection
15 {
19  const CUSTOMER_MODEL_NAME = \Magento\Customer\Model\Customer::class;
20 
24  protected $_fieldsetConfig;
25 
29  protected $_modelName;
30 
48  public function __construct(
49  \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
51  \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
52  \Magento\Framework\Event\ManagerInterface $eventManager,
53  \Magento\Eav\Model\Config $eavConfig,
55  \Magento\Eav\Model\EntityFactory $eavEntityFactory,
56  \Magento\Eav\Model\ResourceModel\Helper $resourceHelper,
57  \Magento\Framework\Validator\UniversalFactory $universalFactory,
58  \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot,
59  \Magento\Framework\DataObject\Copy\Config $fieldsetConfig,
60  \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
61  $modelName = self::CUSTOMER_MODEL_NAME
62  ) {
63  $this->_fieldsetConfig = $fieldsetConfig;
64  $this->_modelName = $modelName;
65  parent::__construct(
66  $entityFactory,
67  $logger,
68  $fetchStrategy,
69  $eventManager,
70  $eavConfig,
71  $resource,
72  $eavEntityFactory,
73  $resourceHelper,
74  $universalFactory,
77  );
78  }
79 
85  protected function _construct()
86  {
87  $this->_init($this->_modelName, \Magento\Customer\Model\ResourceModel\Customer::class);
88  }
89 
95  public function groupByEmail()
96  {
97  $this->getSelect()->from(
98  ['email' => $this->getEntity()->getEntityTable()],
99  ['email_count' => new \Zend_Db_Expr('COUNT(email.entity_id)')]
100  )->where(
101  'email.entity_id = e.entity_id'
102  )->group(
103  'email.email'
104  );
105 
106  return $this;
107  }
108 
114  public function addNameToSelect()
115  {
116  $fields = [];
117  $customerAccount = $this->_fieldsetConfig->getFieldset('customer_account');
118  foreach ($customerAccount as $code => $field) {
119  if (isset($field['name'])) {
120  $fields[$code] = $code;
121  }
122  }
123 
124  $connection = $this->getConnection();
125  $concatenate = [];
126  if (isset($fields['prefix'])) {
127  $concatenate[] = $connection->getCheckSql(
128  '{{prefix}} IS NOT NULL AND {{prefix}} != \'\'',
129  $connection->getConcatSql(['LTRIM(RTRIM({{prefix}}))', '\' \'']),
130  '\'\''
131  );
132  }
133  $concatenate[] = 'LTRIM(RTRIM({{firstname}}))';
134  $concatenate[] = '\' \'';
135  if (isset($fields['middlename'])) {
136  $concatenate[] = $connection->getCheckSql(
137  '{{middlename}} IS NOT NULL AND {{middlename}} != \'\'',
138  $connection->getConcatSql(['LTRIM(RTRIM({{middlename}}))', '\' \'']),
139  '\'\''
140  );
141  }
142  $concatenate[] = 'LTRIM(RTRIM({{lastname}}))';
143  if (isset($fields['suffix'])) {
144  $concatenate[] = $connection->getCheckSql(
145  '{{suffix}} IS NOT NULL AND {{suffix}} != \'\'',
146  $connection->getConcatSql(['\' \'', 'LTRIM(RTRIM({{suffix}}))']),
147  '\'\''
148  );
149  }
150 
151  $nameExpr = $connection->getConcatSql($concatenate);
152 
153  $this->addExpressionAttributeToSelect('name', $nameExpr, $fields);
154 
155  return $this;
156  }
157 
163  public function getSelectCountSql()
164  {
165  $select = parent::getSelectCountSql();
166  $select->resetJoinLeft();
167 
168  return $select;
169  }
170 
178  protected function _getAllIdsSelect($limit = null, $offset = null)
179  {
180  $idsSelect = parent::_getAllIdsSelect($limit, $offset);
181  $idsSelect->resetJoinLeft();
182  return $idsSelect;
183  }
184 }
$fields
Definition: details.phtml:14
$resource
Definition: bulk.php:12
$logger
__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\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot, \Magento\Framework\DataObject\Copy\Config $fieldsetConfig, \Magento\Framework\DB\Adapter\AdapterInterface $connection=null, $modelName=self::CUSTOMER_MODEL_NAME)
Definition: Collection.php:48
addExpressionAttributeToSelect($alias, $expression, $attribute)
$connection
Definition: bulk.php:13
$code
Definition: info.phtml:12