getProperty() public method

Returns property contents.
public getProperty ( string $name ) : mixed | Nextras\Orm\Entity\IPropertyContainer
$name string
return mixed | Nextras\Orm\Entity\IPropertyContainer
コード例 #1
0
ファイル: OneHasMany.php プロジェクト: nextras/orm
 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;
 }
コード例 #2
0
ファイル: ManyHasMany.php プロジェクト: nextras/orm
 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();
 }
コード例 #3
0
ファイル: ToArrayConverter.php プロジェクト: Zarganwar/orm
 /**
  * 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;
 }
コード例 #4
0
ファイル: PersistanceHelper.php プロジェクト: pryznar/orm
 /**
  * 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;
 }
コード例 #5
0
ファイル: OneHasMany.php プロジェクト: JanTvrdik/nextras-orm
 protected function updateRelationshipRemove(IEntity $entity)
 {
     $this->updatingReverseRelationship = TRUE;
     $entity->getProperty($this->metadata->relationship->property)->setInjectedValue(NULL);
     $this->updatingReverseRelationship = FALSE;
 }
コード例 #6
0
ファイル: DbalMapper.php プロジェクト: nextras/orm
 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;
 }
コード例 #7
0
ファイル: ArrayMapper.php プロジェクト: nextras/orm
 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;
 }
コード例 #8
0
ファイル: ManyHasMany.php プロジェクト: JanTvrdik/nextras-orm
 protected function updateRelationshipRemove(IEntity $entity)
 {
     $otherSide = $entity->getProperty($this->metadata->relationship->property);
     $otherSide->collection = NULL;
     $otherSide->toRemove[spl_object_hash($this->parent)] = $this->parent;
 }
コード例 #9
0
ファイル: PersistenceHelper.php プロジェクト: nextras/orm
 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;
     }
 }
コード例 #10
0
 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;
 }
コード例 #11
0
ファイル: RemovalHelper.php プロジェクト: nextras/orm
 /**
  * @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.");
             }
         }
     }
 }
コード例 #12
0
 public function __construct(IEntity $parent, PropertyMetadata $metadata)
 {
     $this->container = $parent->getProperty($metadata->args[0]);
     $this->metadata = $metadata;
 }
コード例 #13
0
ファイル: RemovalHelper.php プロジェクト: pryznar/orm
 /**
  * @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.");
             }
         }
     }
 }