Returns property contents.
public getProperty ( string $name ) : mixed | Nextras\Orm\Entity\IPropertyContainer | ||
$name | string | |
Результат | mixed | Nextras\Orm\Entity\IPropertyContainer |
protected function updateRelationshipRemove(IEntity $entity) { if (!$this->metadata->relationship->property) { return; } $this->updatingReverseRelationship = true; $entity->getProperty($this->metadata->relationship->property)->setInjectedValue(null); $this->updatingReverseRelationship = false; }
protected function updateRelationshipRemove(IEntity $entity) { if (!$this->metadata->relationship->property) { return; } $otherSide = $entity->getProperty($this->metadata->relationship->property); $otherSide->collection = null; $otherSide->toRemove[spl_object_hash($this->parent)] = $this->parent; $otherSide->modify(); }
/** * Converts IEntity to array * @param IEntity $entity * @param int $type * @param int $recursionLevel * @return array|null */ public static function toArray(IEntity $entity, $type = IEntity::TO_ARRAY_RELATIONSHIP_AS_IS, $recursionLevel = 0) { if ($recursionLevel >= static::$maxRecursionLevel) { return NULL; } $return = []; $metadata = $entity->getMetadata(); foreach ($metadata->getStorageProperties() as $name) { if ($name === 'id' && !$entity->isPersisted()) { $value = NULL; } elseif ($type !== IEntity::TO_ARRAY_LOADED_RELATIONSHIP_AS_IS) { $value = $entity->getValue($name); } else { $property = $entity->getProperty($name); if ($property instanceof IRelationshipContainer) { if (!$property->isLoaded()) { $value = $property->getPrimaryValue(); } else { $value = $entity->getValue($name); } } elseif ($property instanceof IRelationshipCollection) { if (!$property->isLoaded()) { continue; } else { $value = $entity->getValue($name); } } else { $value = $entity->getValue($name); } } if ($value instanceof IEntity) { if ($type === IEntity::TO_ARRAY_RELATIONSHIP_AS_ID) { $value = $value->id; } elseif ($type === IEntity::TO_ARRAY_RELATIONSHIP_AS_ARRAY) { $value = static::toArray($value, $type, $recursionLevel + 1); } } elseif ($value instanceof IRelationshipCollection) { if ($type === IEntity::TO_ARRAY_RELATIONSHIP_AS_ID) { $collection = []; foreach ($value as $collectionEntity) { $collection[] = $collectionEntity->id; } $value = $collection; } elseif ($type === IEntity::TO_ARRAY_RELATIONSHIP_AS_ARRAY) { $collection = []; foreach ($value as $collectionEntity) { $collection[] = static::toArray($collectionEntity, $type, $recursionLevel + 1); } $value = $collection; } } $return[$name] = $value; } return $return; }
/** * Returns entity relationships as array, 0 => pre, 1 => post * @param IEntity $entity * @return array */ public static function getLoadedRelationships(IEntity $entity) { $isPersisted = $entity->isPersisted(); $return = [[], []]; foreach ($entity->getMetadata()->getProperties() as $propertyMeta) { if ($propertyMeta->relationship === NULL || !$propertyMeta->relationship->cascade['persist']) { continue; } $name = $propertyMeta->name; $rawValue = $entity->getRawProperty($name); if (!is_object($rawValue) && ($propertyMeta->isNullable || $isPersisted)) { continue; } $property = $entity->getProperty($name); if ($property instanceof IRelationshipContainer) { if (!$property->isLoaded() && $isPersisted) { continue; } $value = $entity->getValue($name); if ($value) { if ($propertyMeta->relationship->type === Relationship::ONE_HAS_ONE && !$propertyMeta->relationship->isMain) { $return[1][$name] = $value; } else { $return[0][$name] = $value; } } } elseif ($property instanceof IRelationshipCollection) { if (!$property->isLoaded() && $isPersisted) { continue; } $value = $entity->getValue($name); if ($value) { $return[1][$name] = $value; } } } return $return; }
protected function updateRelationshipRemove(IEntity $entity) { $this->updatingReverseRelationship = TRUE; $entity->getProperty($this->metadata->relationship->property)->setInjectedValue(NULL); $this->updatingReverseRelationship = FALSE; }
protected function entityToArray(IEntity $entity) { $return = []; $metadata = $entity->getMetadata(); foreach ($metadata->getProperties() as $name => $metadataProperty) { if ($metadataProperty->isVirtual) { continue; } elseif ($metadataProperty->isPrimary && ($entity->isPersisted() || !$entity->hasValue($name))) { continue; } elseif ($entity->isPersisted() && !$entity->isModified($name)) { continue; } if ($metadataProperty->relationship !== null) { $rel = $metadataProperty->relationship; $canSkip = $rel->type === Relationship::ONE_HAS_MANY || $rel->type === Relationship::MANY_HAS_MANY || $rel->type === Relationship::ONE_HAS_ONE && !$rel->isMain; if ($canSkip) { continue; } } $property = $entity->getProperty($name); if ($property instanceof IProperty) { $value = $property->getRawValue(); } else { $value = $entity->getValue($name); } $return[$name] = $value; } return $return; }
protected function entityToArray(IEntity $entity) { $return = []; $metadata = $entity->getMetadata(); foreach ($metadata->getProperties() as $name => $metadataProperty) { if ($metadataProperty->isVirtual) { continue; } elseif ($metadataProperty->isPrimary && !$entity->hasValue($name)) { continue; } $property = $entity->getProperty($name); if ($property instanceof IRelationshipCollection) { continue; } if ($property instanceof IProperty) { $value = $property->getRawValue(); } else { $value = $entity->getValue($name); } $return[$name] = $value; } return $return; }
protected function updateRelationshipRemove(IEntity $entity) { $otherSide = $entity->getProperty($this->metadata->relationship->property); $otherSide->collection = NULL; $otherSide->toRemove[spl_object_hash($this->parent)] = $this->parent; }
protected static function addRelationshipToQueue(IEntity $entity, PropertyMetadata $propertyMeta, IModel $model) { $isPersisted = $entity->isPersisted(); $rawValue = $entity->getRawProperty($propertyMeta->name); if ($rawValue === null && ($propertyMeta->isNullable || $isPersisted)) { return; } elseif (!$entity->getProperty($propertyMeta->name)->isLoaded() && $isPersisted) { return; } $value = $entity->getValue($propertyMeta->name); $rel = $propertyMeta->relationship; if ($value instanceof IEntity && !$value->isPersisted() && ($rel->type !== Relationship::ONE_HAS_ONE || $rel->isMain)) { self::visitEntity($value, $model); } elseif ($value !== null) { self::$inputQueue[] = $value; } }
protected function entityToArray(IEntity $entity) { $return = []; $metadata = $entity->getMetadata(); foreach ($metadata->getProperties() as $name => $metadataProperty) { if ($metadataProperty->isVirtual) { continue; } if ($metadataProperty->relationship !== NULL) { $rel = $metadataProperty->relationship; $canSkip = $rel->type === PropertyRelationshipMetadata::ONE_HAS_MANY || $rel->type === PropertyRelationshipMetadata::MANY_HAS_MANY || $rel->type === PropertyRelationshipMetadata::ONE_HAS_ONE_DIRECTED && !$rel->isMain; if ($canSkip) { continue; } } $property = $entity->getProperty($name); if ($property instanceof IProperty) { $value = $property->getRawValue(); } else { $value = $entity->getValue($name); } $return[$name] = $value; } return $return; }
/** * @param IEntity $entity * @param PropertyMetadata[] $metadata * @parma IModel $model * @param array $pre */ private static function setNulls($entity, array $metadata, IModel $model, array &$pre) { foreach ($metadata as $propertyMeta) { $type = $propertyMeta->relationship->type; $name = $propertyMeta->name; $reverseRepository = $model->getRepository($propertyMeta->relationship->repository); $reverseProperty = $propertyMeta->relationship->property ? $reverseRepository->getEntityMetadata()->getProperty($propertyMeta->relationship->property) : null; if ($type === Relationship::MANY_HAS_MANY) { /** @var ManyHasMany $property */ $property = $entity->getProperty($name); $pre[] = $property; if ($reverseProperty !== null) { foreach ($property as $reverseEntity) { $pre[] = $reverseEntity->getProperty($reverseProperty->name); } } $entity->setValue($name, []); } elseif ($type === Relationship::MANY_HAS_ONE || $type === Relationship::ONE_HAS_ONE && $propertyMeta->relationship->isMain) { /** @var ManyHasOne|OneHasOne $property */ $property = $entity->getProperty($name); if ($reverseProperty !== null && $entity->hasValue($name)) { $pre[] = $entity->getValue($name)->getProperty($reverseProperty->name); } $property->set(null, true); } else { // $type === Relationship::ONE_HAS_MANY or // $type === Relationship::ONE_HAS_ONE && !$isMain if (!$entity->hasValue($name) || $reverseProperty === null) { continue; } if ($reverseProperty->isNullable) { if ($type === Relationship::ONE_HAS_MANY) { foreach ($entity->getValue($name) as $subValue) { $pre[] = $subValue; } $entity->setValue($name, []); } else { $pre[] = $entity->getValue($name); $entity->getProperty($name)->set(null, true); } } else { if ($type === Relationship::ONE_HAS_MANY && $entity->getValue($name)->count() === 0) { continue; } $entityClass = get_class($entity); $reverseEntityClass = $propertyMeta->relationship->entity; $primaryValue = $entity->getValue('id'); $primaryValue = is_array($primaryValue) ? '[' . implode(', ', $primaryValue) . ']' : $primaryValue; throw new InvalidStateException("Cannot remove {$entityClass}::\$id={$primaryValue} because {$reverseEntityClass}::\${$reverseProperty->name} cannot be a null."); } } } }
public function __construct(IEntity $parent, PropertyMetadata $metadata) { $this->container = $parent->getProperty($metadata->args[0]); $this->metadata = $metadata; }
/** * @param IEntity $entity * @param PropertyMetadata[] $metadata * @parma IModel $model * @param array $pre */ private static function setNulls($entity, array $metadata, IModel $model, array &$pre) { foreach ($metadata as $propertyMeta) { $type = $propertyMeta->relationship->type; $name = $propertyMeta->name; if ($type === Relationship::MANY_HAS_MANY) { $entity->setValue($name, []); } elseif ($type === Relationship::MANY_HAS_ONE || $type === Relationship::ONE_HAS_ONE && $propertyMeta->relationship->isMain) { $entity->getProperty($name)->set(NULL, TRUE); } else { // $type === Relationship::ONE_HAS_MANY or // $type === Relationship::ONE_HAS_ONE && !$isPrimary $reverseRepository = $model->getRepository($propertyMeta->relationship->repository); $reverseProperty = $reverseRepository->getEntityMetadata()->getProperty($propertyMeta->relationship->property); if ($reverseProperty->isNullable) { if ($entity->hasValue($name)) { if ($type === Relationship::ONE_HAS_MANY) { foreach ($entity->getValue($name) as $subValue) { $pre[] = $subValue; } $entity->getValue($name)->set([]); } else { $pre[] = $entity->getValue($name); $entity->setValue($name, NULL); } } } else { $entityClass = get_class($entity); $reverseEntityClass = $propertyMeta->relationship->entity; $primaryValue = $entity->getValue('id'); $primaryValue = is_array($primaryValue) ? '[' . implode(', ', $primaryValue) . ']' : $primaryValue; throw new InvalidStateException("Cannot remove {$entityClass}::\$id={$primaryValue} because {$reverseEntityClass}::\${$reverseProperty->name} cannot be a null."); } } } }