getMetadata() публичный Метод

Returns entity metadata.
public getMetadata ( ) : EntityMetadata
Результат Nextras\Orm\Entity\Reflection\EntityMetadata
Пример #1
0
 /**
  * Returns entity relationships as array, 0 => pre, 1 => post, 2 => nulls
  * @param  IEntity  $entity
  * @return array
  */
 public static function getRelationships(IEntity $entity)
 {
     $return = [[], [], []];
     foreach ($entity->getMetadata()->getProperties() as $propertyMeta) {
         if ($propertyMeta->relationship === null) {
             continue;
         }
         $name = $propertyMeta->name;
         if (!$propertyMeta->relationship->cascade['remove']) {
             $return[2][$name] = $propertyMeta;
             continue;
         }
         $rawValue = $entity->getRawValue($name);
         if ($rawValue === null && $propertyMeta->isNullable) {
             continue;
         }
         $property = $entity->getProperty($name);
         if ($property instanceof IRelationshipContainer) {
             $value = $entity->getValue($name);
             if ($value) {
                 if ($propertyMeta->relationship->type === Relationship::ONE_HAS_ONE && !$propertyMeta->relationship->isMain) {
                     $return[0][$name] = $value;
                 } else {
                     $return[1][$name] = $value;
                 }
             }
         } elseif ($property instanceof IRelationshipCollection) {
             $return[0][$name] = $entity->getValue($name);
         }
     }
     return $return;
 }
Пример #2
0
 /**
  * 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;
 }
Пример #3
0
 public function __construct(Nextras\Orm\Entity\IEntity $entity, Nextras\Orm\Repository\IRepository $repository)
 {
     parent::__construct();
     $this->entity = $entity;
     $this->repository = $repository;
     $this->metadata = $entity->getMetadata();
     $this->mapper = $repository->getMapper();
     $this->model = $repository->getModel();
     $this->monitor(Ytnuk\Orm\Form::class);
     $repository->attach($entity);
 }
 protected function fill(IEntity $entity, array $params)
 {
     foreach ($entity->getMetadata()->getProperties() as $property) {
         if ($property->isReadonly) {
             continue;
         }
         $key = $property->name;
         if (array_key_exists($key, $params)) {
             $value = $params[$key];
             unset($params[$key]);
         } elseif ($property->isNullable || $entity->hasValue($key)) {
             continue;
         } else {
             $value = $this->random($property);
         }
         $entity->{$key} = $value;
     }
 }
Пример #5
0
 /**
  * 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->getProperties() as $name => $metadataProperty) {
         if (!$entity->hasValue($name)) {
             $value = null;
         } else {
             $value = $entity->getValue($name);
         }
         if ($value instanceof IEntity) {
             if ($type === IEntity::TO_ARRAY_RELATIONSHIP_AS_ID) {
                 $value = $value->getValue('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->getValue('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;
 }
Пример #6
0
 /**
  * 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;
 }
Пример #7
0
 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;
 }
Пример #8
0
 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 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;
 }
Пример #10
0
 public function persist(IEntity $entity)
 {
     if (!$entity->isModified()) {
         $entity->id;
     }
     $this->beginTransaction();
     $id = $entity->getValue('id', TRUE);
     $data = $entity->toArray(IEntity::TO_ARRAY_LOADED_RELATIONSHIP_AS_IS);
     $storageProperties = $entity->getMetadata()->getStorageProperties();
     foreach ($data as $key => $value) {
         if (!in_array($key, $storageProperties, TRUE) || $value instanceof IRelationshipCollection) {
             unset($data[$key]);
         }
         if ($value instanceof IEntity) {
             $data[$key] = $value->id;
         }
     }
     unset($data['id']);
     $data = $this->getStorageReflection()->convertEntityToStorage($data);
     if (!$entity->isPersisted()) {
         $this->databaseContext->query('INSERT INTO ' . $this->getTableName() . ' ', $data);
         if ($id) {
             return $id;
         } else {
             return $this->databaseContext->getInsertId($this->databaseStructure->getPrimaryKeySequence($this->getTableName()));
         }
     } else {
         $primary = [];
         $id = (array) $id;
         foreach ($this->getStorageReflection()->getStoragePrimaryKey() as $key) {
             $primary[$key] = array_shift($id);
         }
         $this->databaseContext->query('UPDATE ' . $this->getTableName() . ' SET', $data, 'WHERE ?', $primary);
         return $entity->id;
     }
 }