Esempio n. 1
0
 /**
  Process all search query joins
 *
 * @param \Phalcon\Mvc\Model\Relation $relation
 * @param array $joinPath
 * @return array|bool
 */
 protected function _processJoins(Relation $relation, array $joinPath)
 {
     $refModel = $relation->getReferencedModel();
     $refModel = new $refModel();
     $refFields = $relation->getReferencedFields();
     $options = $relation->getOptions();
     $dataSourceIn = $refModel->queryBuilder();
     $dataSourceIn->setColumn($refFields);
     $relation = array_shift($joinPath);
     if ($joinPath) {
         if (!($ids = $this->_processJoins($relation, $joinPath))) {
             return false;
         }
         $fields = $relation->getFields();
         $where = "(" . $fields . " IN (" . implode($ids, ",") . "))";
     } else {
         //$dataSourceIn->joinPath($joinPath);
         $where = $this->_filter->filterWhere($dataSourceIn);
     }
     $dataSourceIn->andWhere($where);
     //$dataSourceIn->columnsId();
     $result = $dataSourceIn->getQuery()->execute()->toArray();
     if (count($result) == 0) {
         return false;
     }
     $ids = [];
     $adapter = $dataSourceIn->getModel()->getReadConnection();
     foreach ($result as $row) {
         $ids[] = $adapter->escapeString($row[$refFields]);
     }
     return $ids;
 }
Esempio n. 2
0
 protected final function hasMany(NgModelBase $model, ModelRelation $relation)
 {
     // check options for alias
     $opts = $relation->getOptions();
     if (!isset($opts["alias"])) {
         return;
     }
     // build needed variable(s)
     $references = $relation->getReferencedFields();
     $modelRelation = $relation->getReferencedModel();
     $query = new Query();
     $query->addCondition(new SimpleCondition($references, Operator::OP_EQUALS, $model->getId()));
     // fetch resultset
     try {
         $handler = new Crud();
         /** @type Resultset $resultSet */
         $resultSet = $handler->read(new $modelRelation(), $query, false);
         unset($handler);
     } catch (CrudException $e) {
         throw new Exception($e->getMessage());
     }
     // check and prepare data.links
     if (!isset($this->data["links"][$references])) {
         $this->data["links"][$references] = array();
     }
     // check and prepare linked
     if (!isset($this->linked[$references])) {
         $this->linked[$references] = array();
     }
     foreach ($resultSet as $ngModel) {
         /** @type NgModelBase $ngModel */
         // check if this model already populated
         if (in_array($ngModel->getId(), $this->hasManyIds)) {
             continue;
         }
         // check if this model already in our data.links
         if (in_array($ngModel->getId(), $this->data["links"][$references])) {
             continue;
         }
         // put relation id on data.links
         $this->data["links"][$references][] = (int) $ngModel->getId();
         // envelope model into relation data
         $relationData = $this->envelope->envelope($ngModel);
         // check if relationData already in our linked
         if (in_array($relationData, $this->linked[$references])) {
             continue;
         }
         // put relation data on our linked
         $this->linked[$references][] = $relationData;
     }
 }