/** * Set field to current table by many to many rule path. * * @param string|array $path * @param string $fieldAlias * @param string $tableField * @param string $orderBy * @param string $separator * @return \Engine\Mvc\Model\Query\Builder */ public function columnsJoinMany($path, $fieldAlias = null, $tableField = null, $orderBy = null, $separator = null) { if (!$path) { throw new \Engine\Exception("Non empty path is required, model '" . get_class($this->_model) . "'"); } if (!is_array($path)) { $path = [$path]; } $relationPath = $this->_model->getRelationPath($path); $this->joinPath($relationPath); $this->groupBy($this->getAlias() . "." . $this->_model->getPrimary()); $prevRef = array_pop($relationPath); $refModel = $prevRef->getReferencedModel(); $refOptions = $prevRef->getOptions(); $refAlias = isset($refOptions['alias']) ? $refOptions['alias'] : $refModel; if ($fieldAlias == null) { $fieldAlias = $refAlias; } $refModel = new $refModel(); $field = $tableField !== null ? $tableField : $refModel->getNameExpr(); if ($separator === null) { $separator = self::SEPARATOR; } if ($tableField == self::COUNT) { $this->setColumn("COUNT({$refAlias}.{$refModel->getPrimary()})", $fieldAlias, false); } else { if (!$orderBy) { $orderBy = $refModel->getOrderExpr(); } $this->setColumn("(LEFT(GROUP_CONCAT({$refAlias}.{$field} ORDER BY {$refAlias}.{$orderBy} SEPARATOR '{$separator}'), 250))", $fieldAlias, false); } return $this; }
/** * * * @param $filters * @param \Engine\Mvc\Model $model * @param \Engine\Crud\Container\AbstractContainer $container * @return \Engine\Filter\SearchFilterInterface */ protected function _getSeparateFilters($filters, \Engine\Mvc\Model $model, Container $container) { $path = $this->_path ? $this->_path : $this->_model; $rule = array_shift($path); $relations = $model->getRelationPath($rule); $relation = array_shift($relations); $fields = $relation->getFields(); $refModel = new $relation->getReferencedModel(); $refFields = $relation->getReferencedFields(); $options = $relation->getOptions(); $queryBuilder = $refModel->queryBuilder(); $queryBuilder->columns($refFields); if (count($path) > 0) { $queryBuilder->columnsJoinOne($path, null); $filterPath = $container->getFilter('path', $path, $filters); $queryBuilder->filter($filterPath); } else { $queryBuilder->filter($filters); } $rows = $queryBuilder->getQuery()->execute()->toArray(); $values = []; foreach ($rows as $val) { $values[] = $val[$prevRef['refColumns']]; } return $container->getFilter('in', [$prevRef['columns'] => Criteria::CRITERIA_EQ], $values); }