/** * 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; }
/** * Deletes or clears, depending on the keepRecord argument, the values of the provided table * which have a relation with the provided data * @param zibo\library\orm\definition\ModelTable $modelTable Table definition of the model of the has many relation * @param integer $id Primary key of the data * @param boolean $keepRecord True to clear the link, false to delete the link * @return null */ private function clearHasMany(ModelTable $modelTable, $id, $keepRecord) { $table = new TableExpression($modelTable->getName()); $relationFields = $modelTable->getRelationFields($this->getName()); $fields = $relationFields[ModelTable::BELONGS_TO]; foreach ($fields as $field) { $fieldName = $field->getName(); if ($keepRecord) { $statement = new UpdateStatement(); $statement->addValue(new FieldExpression($fieldName), null); } else { $statement = new DeleteStatement(); } $condition = new SimpleCondition(new FieldExpression($fieldName), new SqlExpression($id), Condition::OPERATOR_EQUALS); $statement->addTable($table); $statement->addCondition($condition); $this->executeStatement($statement); } $model = $this->getModel($modelTable->getName()); $model->clearCache(); }
/** * Gets the foreign keys from the provided model table for the provided relation model. When no foreign keys are found and the relation * model is a localized model, the unlocalized model will be queried for the foreign keys. * @param zibo\library\orm\definition\ModelTable $modelTable Table definition of the model * @param string $relationModelName Model name to get the foreign keys of * @return array Array with ModelField objects * @throws zibo\library\orm\exception\ModelException when there are no foreign keys found the provided model */ private function getForeignKeys(ModelTable $modelTable, $relationModelName) { if (!$relationModelName) { throw new ModelException('Provided relation model name is empty'); } $foreignKeys = $modelTable->getRelationFields($relationModelName, ModelTable::BELONGS_TO); if (!$foreignKeys) { if (preg_match('/' . LocalizedModel::MODEL_SUFFIX . '$/', $relationModelName)) { $relationModelName = substr($relationModelName, 0, strlen(LocalizedModel::MODEL_SUFFIX) * -1); $foreignKeys = $modelTable->getRelationFields($relationModelName, ModelTable::BELONGS_TO); } if (!$foreignKeys) { throw new ModelException('No foreign key found for ' . $relationModelName . ' found in ' . $modelTable->getName()); } } return $foreignKeys; }