/** * @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; } }
/** * @param \obo\Carriers\QueryCarrier $specification * @param string $repositoryName * @param \obo\Entity $owner * @param string $targetEntity */ protected function constructJoinQueryForRelationship(\obo\Carriers\QueryCarrier $specification, $repositoryName, \obo\Entity $owner, $targetEntity) { $targetEntityPropertyNameForSoftDelete = $targetEntity::entityInformation()->propertyNameForSoftDelete; if ($targetEntityPropertyNameForSoftDelete === "") { $specification->join("JOIN [{$repositoryName}] ON [{$owner->entityInformation()->repositoryName}] = " . $this->informationForEntity($owner->entityInformation())["columns"][$owner->entityInformation()->informationForPropertyWithName($owner->entityInformation()->primaryPropertyName)->columnName]["placeholder"] . " AND [{$targetEntity::entityInformation()->repositoryName}] = [{$targetEntity::informationForPropertyWithName($targetEntity::entityInformation()->primaryPropertyName)->columnName}]", $owner->primaryPropertyValue()); } else { $softDeleteJoinQuery = "AND [{$targetEntity::entityInformation()->repositoryName}].[{$targetEntity::informationForPropertyWithName($targetEntityPropertyNameForSoftDelete)->columnName}] = %b"; $specification->join("JOIN [{$repositoryName}] ON [{$owner->entityInformation()->repositoryName}] = " . $this->informationForEntity($owner->entityInformation())["columns"][$owner->entityInformation()->informationForPropertyWithName($owner->entityInformation()->primaryPropertyName)->columnName]["placeholder"] . " AND [{$targetEntity::entityInformation()->repositoryName}] = [{$targetEntity::informationForPropertyWithName($targetEntity::entityInformation()->primaryPropertyName)->columnName}]" . $softDeleteJoinQuery, $owner->primaryPropertyValue(), FALSE); } }
/** * @param \obo\Entity $entity * @return string */ public function identificationKeyForEntity(\obo\Entity $entity) { return $entity->className() . $entity->valueForPropertyWithName($entity->entityInformation()->primaryPropertyName); }
/** * @param \obo\Entity $entity */ public function removeEntity(\obo\Entity $entity) { $primaryPropertyValue = $entity->primaryPropertyValue(); $repositoryName = $entity->entityInformation()->repositoryName; unset($this->data[$repositoryName][$primaryPropertyValue]); }
/** * @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); }