예제 #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
 /**
  * @param \obo\Entity $entity
  * @return string
  */
 public function identificationKeyForEntity(\obo\Entity $entity)
 {
     return $entity->className() . $entity->valueForPropertyWithName($entity->entityInformation()->primaryPropertyName);
 }
예제 #3
0
 /**
  * @param string $name
  * @param mixed $value
  * @throws \obo\Exceptions\PropertyNotFoundException
  */
 public function __set($name, $value)
 {
     throw new \obo\Exceptions\PropertyNotFoundException("Can't write to the property with name '{$name}' in entity '" . $this->_owner->className() . "', it is read-only");
 }