/** * Gets whether this model has a relation with the provided model * @param string $modelName Name of the relation model * @param integer $type Type identifier to get only the provided type * @return boolean True if there is a field with a relation to the provided model, false otherwise */ public function hasRelationWith($modelName, $type = null) { $relations = $this->table->getRelationFields($modelName); if ($type !== null) { return array_key_exists($type, $relations) && !empty($relations[$type]); } return !(empty($relations[ModelTable::HAS_MANY]) && empty($relations[ModelTable::HAS_ONE]) && empty($relations[ModelTable::BELONGS_TO])); }
/** * @expectedException zibo\library\orm\exception\OrmException */ public function testGetRelationFieldsThrowsExceptionWhenInvalidTypeProvided() { $table = new ModelTable('table'); $table->getRelationFields('model', 'test'); }
/** * 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; }