protected function createComponentOneHasOne(Nextras\Orm\Entity\Reflection\PropertyMetadata $metadata, bool $force = FALSE) { if (!$force && !$metadata->relationship->isMain) { return NULL; } if ($this->entity->hasValue($metadata->name)) { $entity = $this->entity->getValue($metadata->name); } else { $repository = $this->model->getRepository($metadata->relationship->repository); $relationshipEntityClass = $repository->getEntityMetadata()->getClassName(); $entity = new $relationshipEntityClass(); } return $this->addComponent($this->form->createComponent($entity), $metadata->name); }
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; } }
/** * 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; }
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; }
/** * @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."); } } } }
/** * @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."); } } } }