14 use Psr\Log\LoggerInterface as Logger;
92 private $_fetchStrategy;
113 parent::__construct($entityFactory);
114 $this->_fetchStrategy = $fetchStrategy;
149 $this->_idFieldName = $fieldName;
172 return $item->getData($field);
174 return parent::_getItemId(
$item);
186 $this->_conn = $conn;
187 $this->_select = $this->_conn->select();
188 $this->_isOrdersRendered =
false;
219 if ($this->_totalRecords ===
null) {
221 $this->_totalRecords = $this->
getConnection()->fetchOne($sql, $this->_bindParams);
242 if (!is_array($part) || !
count($part)) {
249 $countSelect->columns(
new \
Zend_Db_Expr((
"COUNT(DISTINCT ".implode(
", ",
$group).
")")));
262 return $this->_select->__toString();
274 public function setOrder($field, $direction = self::SORT_ORDER_DESC)
276 return $this->_setOrder($field, $direction);
286 public function addOrder($field, $direction = self::SORT_ORDER_DESC)
288 return $this->_setOrder($field, $direction);
298 public function unshiftOrder($field, $direction = self::SORT_ORDER_DESC)
300 return $this->_setOrder($field, $direction,
true);
311 private function _setOrder($field, $direction, $unshift =
false)
313 $this->_isOrdersRendered =
false;
317 unset($this->_orders[$field]);
320 $orders = [$field => $direction];
321 foreach ($this->_orders as $key => $dir) {
326 $this->_orders[$field] = $direction;
338 if ($this->_isFiltersRendered) {
344 foreach ($this->_filters as $filter) {
345 switch ($filter[
'type']) {
347 $condition = $this->_conn->quoteInto($filter[
'field'] .
'=?', $filter[
'value']);
348 $this->_select->orWhere($condition);
351 $this->_select->where($filter[
'value']);
355 $condition = $filter[
'value'];
359 $condition = $this->_conn->quoteInto($filter[
'field'] .
'=?', $filter[
'value']);
360 $this->_select->where($condition);
363 $this->_isFiltersRendered =
true;
387 if (is_array($field)) {
389 foreach ($field as $key =>
$value) {
393 $resultCondition =
'(' . implode(
') ' . \
Magento\Framework\DB\
Select::SQL_OR .
' (', $conditions) .
')';
426 if (isset($mapper[
'fields'][$field])) {
427 $mappedField = $mapper[
'fields'][$field];
429 $mappedField = $field;
442 if (isset($this->_map)) {
482 return $this->
getConnection()->prepareSqlCondition($fieldName, $condition);
503 if (!$this->_isOrdersRendered) {
504 foreach ($this->_orders as $field => $direction) {
505 $this->_select->order(
new \
Zend_Db_Expr($field .
' ' . $direction));
507 $this->_isOrdersRendered =
true;
520 if ($this->_pageSize) {
521 $this->_select->limitPage($this->
getCurPage(), $this->_pageSize);
535 $this->_select->distinct($flag);
556 public function load($printQuery =
false, $logQuery =
false)
579 if (is_array(
$data)) {
585 $item->addData($row);
615 if (
null === $this->_fetchStmt) {
620 $data = $this->_fetchStmt->fetch();
641 protected function _toOptionArray($valueField =
null, $labelField =
'name', $additional = [])
643 if ($valueField ===
null) {
646 return parent::_toOptionArray($valueField, $labelField, $additional);
658 if ($valueField ===
null) {
661 return parent::_toOptionHash($valueField, $labelField);
671 if ($this->_data ===
null) {
718 public function loadData($printQuery =
false, $logQuery =
false)
720 return $this->
load($printQuery, $logQuery);
731 public function printLogQuery($printQuery =
false, $logQuery =
false, $sql =
null)
733 if ($printQuery || $this->
getFlag(
'print_query')) {
734 echo $sql ===
null ? $this->
getSelect()->__toString() : $sql;
737 if ($logQuery || $this->
getFlag(
'log_query')) {
751 $this->_logger->info($sql ===
null ? $this->
getSelect()->__toString() : $sql);
766 $this->extensionAttributesJoinProcessor =
null;
778 $data = $this->_fetchStrategy->fetchAll(
$select, $this->_bindParams);
779 if ($this->extensionAttributesJoinProcessor) {
780 foreach (
$data as $key => $dataItem) {
781 $data[$key] = $this->extensionAttributesJoinProcessor->extractExtensionAttributes(
782 $this->_itemObjectClass,
800 if ($this->_map ===
null) {
801 $this->_map = [
$group => []];
817 if (is_object($this->_select)) {
846 $joinOn = $this->getMainTableAlias() .
'.' . $join->
getJoinField()
856 $fieldWIthDbPrefix = $selectField[JoinDataInterface::SELECT_FIELD_WITH_DB_PREFIX];
857 $columns[$selectField[JoinDataInterface::SELECT_FIELD_INTERNAL_ALIAS]] = $fieldWIthDbPrefix;
858 $this->
addFilterToMap($selectField[JoinDataInterface::SELECT_FIELD_EXTERNAL_ALIAS], $fieldWIthDbPrefix);
881 private function getMainTableAlias()
884 if ($tableMetadata[
'joinType'] ==
'from') {
888 throw new \LogicException(
"Main table cannot be identified.");
899 [
'_fetchStrategy',
'_logger',
'_conn',
'extensionAttributesJoinProcessor']
912 $this->_fetchStrategy =
$objectManager->get(FetchStrategyInterface::class);
913 $this->_conn =
$objectManager->get(ResourceConnection::class)->getConnection();
addFilterToMap($filter, $alias, $group='fields')
addBindParam($name, $value)
elseif(isset( $params[ 'redirect_parent']))
addFieldToFilter($field, $condition=null)
printLogQuery($printQuery=false, $logQuery=false, $sql=null)
_toOptionArray($valueField=null, $labelField='name', $additional=[])
_fetchAll(Select $select)
unshiftOrder($field, $direction=self::SORT_ORDER_DESC)
getSelectSql($stringMode=false)
_setIdFieldName($fieldName)
$extensionAttributesJoinProcessor
setConnection(\Magento\Framework\DB\Adapter\AdapterInterface $conn)
loadData($printQuery=false, $logQuery=false)
loadWithFilter($printQuery=false, $logQuery=false)
_getConditionSql($fieldName, $condition)
_getItemId(\Magento\Framework\DataObject $item)
load($printQuery=false, $logQuery=false)
__construct(EntityFactoryInterface $entityFactory, Logger $logger, FetchStrategyInterface $fetchStrategy, \Magento\Framework\DB\Adapter\AdapterInterface $connection=null)
addOrder($field, $direction=self::SORT_ORDER_DESC)
setOrder($field, $direction=self::SORT_ORDER_DESC)
joinExtensionAttribute(JoinDataInterface $join, JoinProcessorInterface $extensionAttributesJoinProcessor)
_toOptionHash($valueField=null, $labelField='name')
_getConditionFieldName($fieldName)
_translateCondition($field, $condition)
beforeAddLoadedItem(\Magento\Framework\DataObject $item)
addItem(\Magento\Framework\DataObject $item)
getCurPage($displacement=0)
if(!isset($_GET['name'])) $name