/** * Queries for has many relation fields with a link model * @param ModelQuery $query Query for the has many data * @param string $fieldName Name of the field which will contain the has many result * @param string $foreignKey Name of the foreign key * @return array Model query result for the has many field */ private function queryHasManyWithLinkModel(ModelQuery $query, ModelMeta $meta, $fieldName, $foreignKey) { if ($query->getRecursiveDepth() == 1) { $order = $meta->getRelationOrder($fieldName); if ($order != null) { $query->addOrderBy($order); } } $result = array(); $queryResult = $query->query(); foreach ($queryResult as $data) { if (!isset($data->{$foreignKey})) { continue; } if (isset($data->{$foreignKey}->id)) { $result[$data->{$foreignKey}->id] = $data->{$foreignKey}; } else { $result[] = $data->{$foreignKey}; } } return $result; }
/** * Sets the model query to this parser and initializes the parser for this model query. * @param zibo\library\orm\query\ModelQuery $modelQuery * @return null */ private function setModelQuery(ModelQuery $modelQuery) { $this->meta = $modelQuery->getModel()->getMeta(); $this->recursiveDepth = $modelQuery->getRecursiveDepth(); $this->localize = false; $this->locale = $modelQuery->getLocale(); $this->includeUnlocalizedData = $modelQuery->willIncludeUnlocalizedData(); $this->addIsLocalizedOrder = $modelQuery->willAddIsLocalizedOrder(); $this->fields = array(); $this->recursiveBelongsToFields = array(); $this->recursiveHasFields = array(); $this->tables = array(); $this->fieldJoins = array(); $this->conditionJoins = array(); $this->addTable($this->meta->getName(), self::ALIAS_SELF); $this->addTable($this->meta->getName() . LocalizedModel::MODEL_SUFFIX, self::ALIAS_SELF_LOCALIZED); $this->statement = new SelectStatement(); $this->statement->addTable($this->tables[self::ALIAS_SELF]); $joins = $modelQuery->getJoins(); foreach ($joins as $join) { $table = $join->getTable(); $this->addTable($table->getModelName(), $table->getAlias()); } }