/** * 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))); } }
/** * 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); $metadata = Metadata::getMetadata($targetEntity); if (empty($name)) { $this->name = lcfirst($metadata->name); } else { $this->name = $name; } if (empty($column)) { $this->column = Tools::underscore($metadata->name . ucfirst($metadata->primaryKey)); } else { $this->column = $column; } }
/** * Contstuctor * * @param array|ArrayAccess $data */ public function __construct($data = NULL) { if ($data instanceof \ArrayAccess) { $data = (array) $data; } elseif (!is_array($data) && $data != NULL) { throw new \InvalidArgumentException("Data for entity must be array accesible."); } $metadata = Metadata::getMetadata(get_called_class()); if (count($data) > 0) { foreach ($metadata->columns as $column) { $name = Tools::underscore($column->name); if (array_key_exists($name, $data)) { $this->{$column->name} = $column->convertToPHPValue($data[$name]); } } } }
public function testUnderscore1() { $this->assertEquals('active_mapper', Tools::underscore('ActiveMapper')); $this->assertEquals('nette_framework', Tools::underscore('NetteFramework')); $this->assertEquals('dibi', Tools::underscore('dibi')); $this->assertEquals('dibi', Tools::underscore('Dibi')); }
/** * Get entity instance with default data * * @param ActiveMapper\Manager $em * @param array $data * @return mixed */ public function getInstance(Manager $em, $data) { if (!is_array($data) && !$data instanceof \ArrayAccess) { throw new \InvalidArgumentException("Get instance data must be array or implement ArrayAccess."); } $ref = ClassReflection::from($this->entity); if ($this->hasProxy()) { $instance = $ref->newInstance($data); } else { $instance = $ref->newInstance(); foreach ($this->columns as $column) { $tmpName = Tools::underscore($column->name); if (isset($data[$tmpName])) { $prop = $ref->getProperty($column->name); $prop->setAccessible(TRUE); $prop->setValue($instance, $column->convertToPHPValue($data[$tmpName])); $prop->setAccessible(FALSE); } } } if (count($this->getAssociations()) > 0) { $propRef = $ref->getProperty('_associations'); $propRef->setAccessible(TRUE); $associations = $propRef->getValue($instance); foreach ($this->getAssociations() as $assoc) { $associations[$assoc->name] = new Associations\LazyLoad($em, $this->entity, $assoc->name, $data); } $propRef->setValue($instance, $associations); $propRef->setAccessible(FALSE); } return $instance; }
/** * Method overload for findBy... * * @param string $name * @param array $args * @return mixed * @throws InvalidArgumentException */ public function __call($name, $args) { if (strncmp($name, 'findBy', 6) === 0 && strlen($name) > 6) { $name = lcfirst(substr($name, 6)); $metadata = Metadata::getMetadata($this->entity); $identityMap = $this->em->getIdentityMap($this->entity); return $identityMap->map($this->em->connection->select("*")->from($metadata->tableName)->where("[" . Tools::underscore($name) . "] = " . $this->getModificator($name), $args[0])->execute()->fetch()); } else { return parent::__call($name, $args); } }