/** * Delete record identified by $keys in the $resources * * @param AnDomainRepositoryAbstract $repository * @param array $keys * @return boolean */ public function delete($repository, $keys) { $context = $this->getCommandContext(); $context->repository = $repository; $context->keys = $keys; $context->query = AnDomainQuery::getInstance($repository, $keys)->delete(); if ($this->getCommandChain()->run('before.delete', $context) !== false) { $context->result = $this->execute($context->query); $this->getCommandChain()->run('after.delete', $context); } return $context->result; }
/** * Fetch an entity. The condition can be a query object, an associative array or an id * of an entity. * * @param mixed $condition The condition for fetching data * @param int $mode The mode of fetching data. Can be single entity, entity set, value, etc * * @return mixed */ public function fetch($condition = null, $mode = AnDomain::FETCH_ENTITY) { $query = AnDomainQuery::getInstance($this, $condition); $context = $this->getCommandContext(); $context->operation = AnDomain::OPERATION_FETCH; $context->query = $query; $context->mode = $mode; $query->fetch_mode = $mode; if ($mode & AnDomain::FETCH_ITEM) { $context->query->limit(1); } $disable_chain = $query->disable_chain; if ($disable_chain) { $this->getCommandChain()->disable(); } if ($this->getCommandChain()->run('before.fetch', $context) !== false) { $result = $context->result ? $context['result'] : $this->_fetchResult($context->query, $mode); $context->result = $result; switch ($mode) { case AnDomain::FETCH_ENTITY: $context->data = $result ? $this->_createEntity($result) : $result; break; case AnDomain::FETCH_ENTITY_SET: case AnDomain::FETCH_ENTITY_LIST: $list = array(); foreach ($result as $data) { $list[] = $this->_createEntity($data); } if ($mode == AnDomain::FETCH_ENTITY_SET) { $list = $this->getService($this->_entityset, array('repository' => $this, 'query' => $context->query, 'data' => $list)); } $context->data = $list; break; default: $context->data = $result; } $this->getCommandChain()->run('after.fetch', $context); } if ($disable_chain) { $this->getCommandChain()->enable(); } return KConfig::unbox($context->data); }
/** * Count Data. * * @param booelan $load If the flag is set to on. If the qurey is set, it will * perform a count query instead of loading all the objects * * @return int */ public function count($load = true) { //if query is set, and the data is not loaded //lets use the query to get the count if (isset($this->_query) && !$this->isLoaded() && !$load) { $query = AnDomainQuery::getInstance($this->getRepository(), $this->_query); return $query->fetchValue('count(*)'); } else { $this->_loadData(); $result = parent::count(); } return $result; }
/** * Validates and apply delete rules for an entity * * @param $entity */ protected function _validateDelete($entity) { $relationships = $entity->getEntityDescription()->getRelationships(); foreach ($relationships as $name => $relationship) { if ($relationship->isManyToOne() || $relationship->isManyToMany()) { continue; } if ($relationship->getDeleteRule() == AnDomain::DELETE_CASCADE || $relationship->getDeleteRule() == AnDomain::DELETE_DESTROY) { $property = $relationship->getName(); $entities = $entity->getData($property); if (!$entities) { continue; } //someone else is responsible for deleteing the relations //good for mass deletion. i.e. node and edges if ($relationship->getDeleteRule() == AnDomain::DELETE_IGNORE) { continue; } else { if ($relationship->getDeleteRule() == AnDomain::DELETE_DESTROY) { if ($relationship->isOneToOne()) { $query = AnDomainQuery::getInstance($relationship->getChildRepository(), $entities->getIdentityId()); } else { $query = $entities->getQuery(); } $relationship->getChildRepository()->destroy($query); continue; } else { if ($relationship->isOneToOne()) { $entities = array($entities); } foreach ($entities as $entity) { //if the cascading fails for the related entities then //nullify the property in the failed entity if (!$entity->delete() && $entity->getObject()) { $entity->set($relationship->getChildKey(), null); } } } } } else { if ($relationship->getDeleteRule() == AnDomain::DELETE_DENY) { //don't state change if there at least one entity left $count = $entity->getData($relationship->getName())->limit(0, 0)->getTotal(); if ($count > 0) { $entity->reset(); return false; } } else { if ($relationship->getDeleteRule() == AnDomain::DELETE_NULLIFY) { //@TODO you should set the values to null directly rather //then instantiating them $entities = $entity->getData($relationship->getName())->fetchSet(); $property = $relationship->getChildKey(); foreach ($entities as $entity) { $entity->set($property, null); } } } } } return true; }