/** * Queries for has relation fields without a link model and sets the result to the provided result * @param array $result Model query result * @param integer $recursiveDepth Recursive depth for the queries * @param string $fieldName Name of the has field * @param zibo\library\orm\definition\field\HasField $field Definition of the field * @return array Model query result with the has fields */ private function queryHasWithoutLinkModel(array $result, ModelMeta $meta, $recursiveDepth, $fieldName, HasField $field) { $locale = $this->getLocale(); $model = $meta->getRelationModel($fieldName); $foreignKey = $meta->getRelationForeignKey($fieldName)->getName(); $isHasOne = $field instanceof HasOneField; $indexOn = null; if (!$isHasOne) { $indexOn = $field->getIndexOn(); } foreach ($result as $index => $data) { $query = $model->createQuery($recursiveDepth, $locale, $this->willIncludeUnlocalizedData); $query->removeFields('{' . $foreignKey . '}'); $query->addCondition('{' . $foreignKey . '} = %1%', $data->id); if ($isHasOne) { $result[$index]->{$fieldName} = $this->queryHasOneWithoutLinkModel($query, $foreignKey, $data); } else { $result[$index]->{$fieldName} = $this->queryHasManyWithoutLinkModel($query, $meta, $fieldName, $foreignKey, $data, $indexOn); } } return $result; }
/** * Parses a has field into the meta * @param zibo\library\orm\definition\field\HasField $field * @param zibo\library\orm\ModelManager $modelManager Instance of the model manager * @return null */ private function parseHasField(HasField $field, ModelManager $modelManager) { $name = $field->getName(); $modelName = $this->table->getName(); $relationModelName = $field->getRelationModelName(); $relationModel = $modelManager->getModel($relationModelName); $relation = new RelationMeta(); $relation->setIsRelationWithSelf($relationModelName == $modelName); $linkModelName = $field->getLinkModelName(); if ($field->isLocalized()) { $localizedMeta = $this->getLocalizedModel()->getMeta(); $localizedField = $localizedMeta->getField($name); $linkModelName = $localizedField->getLinkModelName(); $field->setLinkModelName($linkModelName); $relation->setLinkModelName($linkModelName); } elseif ($linkModelName) { $relation->setLinkModelName($linkModelName); $linkModel = $modelManager->getModel($linkModelName); $linkModelTable = $linkModel->getMeta()->getModelTable(); if (!$relation->isRelationWithSelf()) { $relation->setForeignKey($this->getForeignKey($linkModelTable, $relationModelName)); $relation->setForeignKeyToSelf($this->getForeignKey($linkModelTable, $modelName)); } else { $relation->setForeignKey($this->getForeignKeys($linkModelTable, $modelName)); } } else { $relationModelTable = $relationModel->getMeta()->getModelTable(); $foreignKey = $field->getForeignKeyName(); $relation->setForeignKey($this->getForeignKey($relationModelTable, $modelName, $foreignKey)); } $this->relations[$name] = $relation; if ($field instanceof HasOneField) { $this->hasOne[$name] = $field; return; } if ($linkModelName) { $relation->setIsHasManyAndBelongsToMany(true); } $this->hasMany[$name] = $field; }