/** * Queries for has relation fields with 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 which will contain the result * @param zibo\library\orm\definition\field\HasField $field Definition of the field * @return array Model query result with the has fields */ private function queryHasWithLinkModel(array $result, ModelMeta $meta, $recursiveDepth, $fieldName, HasField $field) { $locale = $this->getLocale(); $linkModel = $meta->getRelationLinkModel($fieldName); $primaryKey = DefinitionModelTable::PRIMARY_KEY; $foreignKey = $meta->getRelationForeignKey($fieldName)->getName(); $foreignKeyToSelf = $meta->getRelationForeignKeyToSelf($fieldName)->getName(); $isHasOne = $field instanceof HasOneField; foreach ($result as $index => $data) { $query = $linkModel->createQuery($recursiveDepth, $locale, $this->willIncludeUnlocalizedData); $query->setOperator(Condition::OPERATOR_OR); $query->setFields('{' . $primaryKey . '}, {' . $foreignKey . '}'); $query->addCondition('{' . $foreignKeyToSelf . '} = %1%', $data->id); if ($isHasOne) { $result[$index]->{$fieldName} = $this->queryHasOneWithLinkModel($query, $foreignKey); } else { $result[$index]->{$fieldName} = $this->queryHasManyWithLinkModel($query, $meta, $fieldName, $foreignKey); } } return $result; }