protected function attached($control) { parent::attached($control); $this->addComponent($this->createComponent($this->entity), 'this'); $this->addGroup('orm.form.action.group'); $action = $this->addContainer('action'); $action->addSubmit('add', 'orm.form.action.add.label')->setDisabled($this->entity->isPersisted()); $action->addSubmit('edit', 'orm.form.action.edit.label')->setDisabled(!$this->entity->isPersisted()); $action->addSubmit('delete', 'orm.form.action.delete.label')->setValidationScope(FALSE)->setDisabled(!$this->entity->isPersisted()); }
protected function getPrimaryValue() { if (!$this->primaryValue && $this->value && $this->value->isPersisted()) { $this->primaryValue = $this->value->getValue('id'); } return $this->primaryValue; }
/** * @return ICollection */ protected function getCollection($forceNew = FALSE) { if ($this->collection !== NULL && !$forceNew) { return $this->collection; } if ($this->parent->isPersisted()) { $collection = $this->createCollection(); } else { $collection = new EmptyCollection(); } if ($this->toAdd || $this->toRemove) { $all = []; foreach ($collection as $entity) { $all[spl_object_hash($entity)] = $entity; } foreach ($this->toAdd as $hash => $entity) { $all[$hash] = $entity; } foreach ($this->toRemove as $hash => $entity) { unset($all[$hash]); } $collection = new ArrayCollection(array_values($all), $this->getTargetRepository()); $collection = $this->applyDefaultOrder($collection); } return $this->collection = $collection; }
/** * 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; }
public function getEntity($collectionName = NULL) { if ($this->value === FALSE) { if (!$this->parent->isPersisted()) { return NULL; } $collection = $this->getCachedCollection($collectionName); $entity = $collection->getEntityIterator($this->parent)[0]; $this->set($entity); } return $this->value; }
/** * 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; }
public function persist(IEntity $entity) { $this->initializeData(); if ($entity->isPersisted()) { $id = $entity->id; } else { $this->lock(); try { $data = $this->readData(); $id = $data ? max(array_keys($data)) + 1 : 1; $data[$id] = NULL; $this->saveData($data); } catch (\Exception $e) { // finally workaround } $this->unlock(); if (isset($e)) { throw $e; } } $this->data[$id] = $entity; return $id; }
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; }
/** @inheritdoc */ public function doPersist(IEntity $entity) { if (!$entity->isModified()) { return; } $isPersisted = $entity->isPersisted(); $this->doFireEvent($entity, $isPersisted ? 'onBeforeUpdate' : 'onBeforeInsert'); $isPersisted && $this->identityMap->remove($entity->getPersistedId()); // id can change in composite key $id = $this->mapper->persist($entity); $entity->fireEvent('onPersist', [$id]); $this->identityMap->add($entity); $this->entitiesToFlush[0][] = $entity; $this->doFireEvent($entity, $isPersisted ? 'onAfterUpdate' : 'onAfterInsert'); $this->doFireEvent($entity, 'onAfterPersist'); }
public function processEntityCache(Nextras\Orm\Entity\IEntity $entity) { if ($entity instanceof Entity && $entity->isPersisted()) { $this->tags = array_merge($this->tags, $entity->getCacheTags(TRUE)); } }
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; } }
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; } }