public function parseMetadata($class, &$fileDependencies) { $this->reflection = new ClassType($class); $this->metadata = new EntityMetadata($class); $this->primaryKey = []; $this->loadProperties($fileDependencies); $this->loadGettersSetters(); // makes id property virtual on entities with composite primary key if ($this->primaryKey && $this->metadata->hasProperty('id')) { $this->metadata->getProperty('id')->isVirtual = TRUE; } $fileDependencies = array_unique($fileDependencies); $this->metadata->setPrimaryKey($this->primaryKey ?: ['id']); return $this->metadata; }
/** * @param string $propertyName * @throws InvalidPropertyException */ public function validateProperty($propertyName) { try { $this->entityMetadata->getProperty($propertyName); } catch (InvalidArgumentException $e) { throw InvalidPropertyException::createNonexistentProperty($propertyName, $e); } }
public function getForeignKey($name) { $metadata = $this->metadata->getProperty($name); if (!in_array($metadata->relationshipType, [PropertyMetadata::RELATIONSHIP_MANY_HAS_ONE, PropertyMetadata::RELATIONSHIP_ONE_HAS_ONE, PropertyMetadata::RELATIONSHIP_ONE_HAS_ONE_DIRECTED], TRUE)) { throw new InvalidArgumentException("There is no HAS ONE relationship in '{$name}' property."); } if (isset($this->validated[$name])) { return $this->data[$name]->getPrimaryValue(); } $this->initDefaultValue($metadata); return $this->data[$name]; }
protected function initPrimaryKey() { $primaryKey = array_values(array_filter(array_map(function (PropertyMetadata $metadata) { return $metadata->isPrimary && !$metadata->isVirtual ? $metadata->name : null; }, $this->metadata->getProperties()))); if (empty($primaryKey)) { throw new InvalidStateException("Entity {$this->reflection->name} does not have defined any primary key."); } elseif (!$this->metadata->hasProperty('id') || !$this->metadata->getProperty('id')->isPrimary) { throw new InvalidStateException("Entity {$this->reflection->name} has to have defined \$id property as {primary} or {primary-proxy}."); } $this->metadata->setPrimaryKey($primaryKey); }
protected function parseFilteredRelationship(PropertyMetadata $property, $args) { $sourceName = ltrim($args[0], '$'); $sourceProperty = $this->metadata->getProperty($sourceName); if ($sourceProperty->relationshipType === PropertyMetadata::RELATIONSHIP_ONE_HAS_ONE || $sourceProperty->relationshipType === PropertyMetadata::RELATIONSHIP_MANY_HAS_ONE) { $property->container = 'Nextras\\Orm\\Entity\\PropertyContainers\\FilteredRelationshipContainerContainer'; } else { $property->container = 'Nextras\\Orm\\Entity\\PropertyContainers\\FilteredRelationshipCollectionContainer'; } $property->args = [$sourceName]; unset($this->storageProperties[$property->name]); }
public function setValues($values, $erase = FALSE) : self { $this->initEntityRelations(); foreach ($values as $property => $value) { if ($this[$property] instanceof Nette\Forms\IControl) { $value = $value === '' ? NULL : $value; if ($this[$property] instanceof Nette\Forms\Controls\Checkbox && !$value && $this->metadata->getProperty($property)->isNullable) { $value = NULL; } $this->entity->setValue($property, $value); } } return parent::setValues($values, $erase); }
public function getRawProperty($name) { $this->metadata->getProperty($name); return isset($this->data[$name]) ? $this->data[$name] : null; }
public function getter($element, $chain, EntityMetadata $sourceEntityMeta) { $column = array_shift($chain); $propertyMeta = $sourceEntityMeta->getProperty($column); // check if property exists $value = $element->hasValue($column) ? $element->getValue($column) : null; if ($value instanceof IRelationshipCollection) { throw new InvalidStateException('You can not sort by hasMany relationship.'); } if (!$chain) { return $this->normalizeValue($value, $propertyMeta); } else { $targetEntityMeta = $this->metadataStorage->get($propertyMeta->relationship->entity); return $this->getter($value, $chain, $targetEntityMeta); } }