Example #1
0
 /**
  * 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;
 }