/** * Returns a new FieldDescription. * * The description is filled with the information retrieved from * * the respective Peer-class and * * the TableMap of the model class. * * @param string $class The FQCN of the model. * @param string $name The column name of the model (should be given as a phpName). * @param array $options A list of options to be passed to the new FielDescription. * * @return FieldDescription */ public function getNewFieldDescriptionInstance($class, $name, array $options = array()) { if (!is_string($name)) { throw new \RunTimeException('The name argument must be a string'); } $fieldDescription = new FieldDescription(); $fieldDescription->setName($name); $fieldDescription->setOptions($options); $fieldDescription->setParentAssociationMappings($this->getParentAssociationMappings($class, $name)); if (!($table = $this->getTable($class))) { return $fieldDescription; // TODO should we throw a logic exception here ? } foreach ($table->getRelations() as $relation) { if ($relation->getType() === \RelationMap::MANY_TO_ONE) { if (strtolower($name) === strtolower($relation->getName())) { $fieldDescription->setAssociationMapping(array('targetEntity' => $relation->getForeignTable()->getClassName(), 'type' => $relation->getType())); } } elseif ($relation->getType() === \RelationMap::ONE_TO_MANY) { if (strtolower($name) === strtolower($relation->getPluralName())) { $fieldDescription->setAssociationMapping(array('targetEntity' => $relation->getForeignTable()->getClassName(), 'type' => $relation->getType())); } } elseif ($relation->getType() === \RelationMap::MANY_TO_MANY) { if (strtolower($name) === strtolower($relation->getPluralName())) { $fieldDescription->setAssociationMapping(array('targetEntity' => $relation->getLocalTable()->getClassName(), 'type' => $relation->getType())); } } } if ($column = $this->getColumn($class, $name)) { $fieldDescription->setType($column->getType()); $fieldDescription->setFieldMapping(array('id' => $column->isPrimaryKey(), 'fieldName' => $column->getPhpName(), 'type' => $column->getType())); } return $fieldDescription; }
public function testGetTargetEntity() { $assocationMapping = array('type' => 'integer', 'targetEntity' => 'someValue'); $field = new FieldDescription(); $this->assertNull($field->getTargetEntity()); $field->setAssociationMapping($assocationMapping); $this->assertEquals('someValue', $field->getTargetEntity()); }