예제 #1
0
 /**
  * @param \obo\Entity $entity
  * @param bool $forced
  * @param bool $triggerEvents
  * @return void
  * @throws \obo\Exceptions\EntityIsDeletedException
  * @throws \obo\Exceptions\EntityIsNotInitializedException
  * @throws \obo\Exceptions\Exception
  * @throws \obo\Exceptions\ServicesException
  */
 public static function saveEntity(\obo\Entity $entity, $forced = false, $triggerEvents = true)
 {
     if (!$entity->isInitialized()) {
         throw new \obo\Exceptions\EntityIsNotInitializedException("Cannot save entity which is not initialized");
     }
     if (!$forced and $entity->isDeleted() and !$entity->isDeletingInProgress()) {
         throw new \obo\Exceptions\EntityIsDeletedException("Cannot save entity which is deleted");
     }
     if ($entity->entityInformation()->repositoryName === null) {
         throw new \obo\Exceptions\Exception("Entity '" . $entity->className() . "' cannot be persisted. No entity storage exists");
     }
     $entity->setSavingInProgress();
     if ($triggerEvents) {
         \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity("beforeSave", $entity);
     }
     if (count($entity->changedProperties($entity->entityInformation()->persistablePropertiesNames, true, true))) {
         if ($entity->isBasedInRepository()) {
             if ($triggerEvents) {
                 \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity("beforeUpdate", $entity);
             }
             $entity->dataStorage()->updateEntity($entity);
             $entity->markUnpersistedPropertiesAsPersisted();
             if ($triggerEvents) {
                 \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity("afterSave", $entity);
             }
             if ($triggerEvents) {
                 \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity("afterUpdate", $entity);
             }
         } else {
             if ($triggerEvents) {
                 \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity("beforeInsert", $entity);
             }
             $entity->dataStorage()->insertEntity($entity);
             $entity->setBasedInRepository(true);
             $entity->markUnpersistedPropertiesAsPersisted();
             if ($triggerEvents) {
                 \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity("afterSave", $entity);
             }
             if ($triggerEvents) {
                 \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity("afterInsert", $entity);
             }
         }
     } else {
         if ($triggerEvents) {
             \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity("afterSave", $entity);
         }
     }
     $entity->setSavingInProgress(false);
 }
예제 #2
0
파일: One.php 프로젝트: Hlavos/obo
 /**
  * @param \obo\Entity $owner
  * @param array $foreignKey
  * @param boolean $autoCreate
  * @return \obo\Entity|null
  */
 public function entityForOwnerForeignKey(\obo\Entity $owner, array $foreignKey, $autoCreate = true)
 {
     if ($owner->primaryPropertyValue() === null) {
         return null;
     }
     $this->owner = $owner;
     $entityClassNameToBeConnected = $this->entityClassNameToBeConnected;
     $entityManagerName = $entityClassNameToBeConnected::entityInformation()->managerName;
     $specification = $entityManagerName::querySpecification();
     if (\count($foreignKey) === 1) {
         $specification->where("{{$foreignKey[0]}} = " . \obo\Interfaces\IQuerySpecification::PARAMETER_PLACEHOLDER, $owner->primaryPropertyValue());
     } else {
         $this->ownerNameInProperty = $foreignKey[1];
         $specification->where("{{$foreignKey[0]}} = " . \obo\Interfaces\IQuerySpecification::PARAMETER_PLACEHOLDER . " AND {{$foreignKey[1]}} = " . \obo\Interfaces\IQuerySpecification::PARAMETER_PLACEHOLDER, $owner->primaryPropertyValue(), $owner->entityInformation()->className);
     }
     if ($entity = $entityManagerName::findEntity($specification, false)) {
         return $entity;
     } else {
         return ($autoCreate and $this->autoCreate and !$owner->isDeleted()) ? $entityManagerName::entityFromArray([$foreignKey[0] => $owner]) : null;
     }
 }