/** * Costructor * * @param string $sourceEntity valid source entity class * @param string $targetEntity valid target entity class * @param bool $mapped is this association mapped/inversed * @param string $name * @param string $joinTable join table name * @param string $joinTargetColumn valid join table target column name * @param string $joinSourceColumn valid join table source column name * @throws InvalidArgumentException */ public function __construct($sourceEntity, $targetEntity, $mapped = TRUE, $name = NULL, $joinTable = NULL, $joinTargetColumn = NULL, $joinSourceColumn = NULL) { parent::__construct($sourceEntity, $targetEntity); $targetMetadata = Metadata::getMetadata($targetEntity); $sourceMetadata = Metadata::getMetadata($sourceEntity); $this->mapped = $mapped; if (empty($name)) { $this->name = lcfirst(Tools::pluralize($targetMetadata->name)); } else { $this->name = $name; } if (empty($joinSourceColumn)) { $this->joinSourceColumn = Tools::underscore($sourceMetadata->name . ucfirst($this->sourceColumn)); } else { $this->joinSourceColumn = $joinSourceColumn; } if (empty($joinTargetColumn)) { $this->joinTargetColumn = Tools::underscore($targetMetadata->name . ucfirst($this->targetColumn)); } else { $this->joinTargetColumn = $joinTargetColumn; } if (!empty($joinTable)) { $this->joinTable = $joinTable; } elseif ($this->mapped) { $this->joinTable = Tools::underscore(Tools::pluralize($sourceMetadata->name) . ucfirst(Tools::pluralize($targetMetadata->name))); } else { $this->joinTable = Tools::underscore(Tools::pluralize($targetMetadata->name) . ucfirst(Tools::pluralize($sourceMetadata->name))); } }
/** * Construct * * @param string $entity entity class name */ public function __construct($entity) { $ref = new ClassReflection($entity); // TODO: verify entity class $this->entity = $entity; if ($pos = strrpos($entity, '\\')) { $pos++; } $this->name = substr($entity, $pos); if ($ref->hasAnnotation('tableName')) { $this->tableName = $ref->getAnnotation('tableName'); } else { $this->tableName = Tools::underscore(Tools::pluralize($this->name)); } foreach ($ref->getProperties() as $property) { if ($property->hasAnnotation('column')) { $annotation = (array) $property->getAnnotation('column'); $datatype = 'ActiveMapper\\DataTypes\\' . $annotation[0]; unset($annotation[0]); $params = array_merge(array($property->name, $property->hasAnnotation('null')), $annotation); if (!\class_exists($datatype)) { throw new \ActiveMapper\InvalidDataTypeException("Data type '{$datatype}' not exist"); } $this->columns[$property->name] = callback(ClassReflection::from($datatype), 'newInstance')->invokeArgs($params); } if ($property->hasAnnotation('primary')) { if ($property->hasAnnotation('column')) { if (empty($this->primaryKey)) { $this->primaryKey = $property->name; } else { throw new \NotImplementedException("Multiple column primary key not implemented '{$entity}'"); } } else { throw new \NotImplementedException("Primary key must be column {$entity}::\${$property->name}"); } } if ($property->hasAnnotation('autoincrement')) { if (!$this->columns[$property->name] instanceof \ActiveMapper\DataTypes\Int) { throw new \ActiveMapper\InvalidDataTypeException("Autoincrement avaiable only for Int data type column {$entity}::\${$property->name}"); } elseif ($property->name == $this->primaryKey) { $this->primaryKeyAutoincrement = TRUE; } else { throw new \NotImplementedException("Auto increment for non primary key column not implemented"); } } } if (!$this->primaryKey) { throw new \LogicException("Entity without primary key not supported '{$this->entity}'"); } if (!$this->hasProxy() && ($ref->hasAnnotation('OneToOne') || $ref->hasAnnotation('OneToOne') || $ref->hasAnnotation('OneToOne') || $ref->hasAnnotation('OneToOne'))) { throw new \LogicException("Entity associations support only on proxy entity"); } }
/** * Costructor * * @param string $sourceEntity valid source entity class * @param string $targetEntity valid target entity class * @param string $name * @param string $column targer/source column name * @throws InvalidArgumentException */ public function __construct($sourceEntity, $targetEntity, $name = NULL, $column = NULL) { parent::__construct($sourceEntity, $targetEntity); if (empty($name)) { $this->name = lcfirst(Tools::pluralize(Metadata::getMetadata($targetEntity)->name)); } else { $this->name = $name; } if (empty($column)) { $metadata = Metadata::getMetadata($sourceEntity); $this->column = Tools::underscore($metadata->name . ucfirst($metadata->primaryKey)); } else { $this->column = $column; } }
public function testPluralize() { $this->assertEquals('dogs', Tools::pluralize('dog')); $this->assertEquals('Dogs', Tools::pluralize('Dog')); $this->assertEquals('DOGs', Tools::pluralize('DOG')); $this->assertEquals('quizzes', Tools::pluralize('quiz')); $this->assertEquals('tomatoes', Tools::pluralize('tomato')); $this->assertEquals('mice', Tools::pluralize('mouse')); $this->assertEquals('people', Tools::pluralize('person')); $this->assertEquals('equipment', Tools::pluralize('equipment')); $this->assertEquals('companies', Tools::pluralize('company')); $this->assertEquals('dogs', Tools::pluralize('dogs')); $this->assertEquals('Dogs', Tools::pluralize('Dogs')); $this->assertEquals('DOGs', Tools::pluralize('DOGs')); $this->assertEquals('quizzes', Tools::pluralize('quizzes')); $this->assertEquals('tomatoes', Tools::pluralize('tomatoes')); $this->assertEquals('mice', Tools::pluralize('mice')); $this->assertEquals('people', Tools::pluralize('people')); $this->assertEquals('equipment', Tools::pluralize('equipment')); $this->assertEquals('companies', Tools::pluralize('companies')); }