示例#1
0
 /**
  * Commits an entity into the data store (database).
  *
  * @param AnDomainEntityAbstract $entity The entity to be committed
  *
  * @return bool Return whether a commit has been succesfull or not
  */
 public function commit($entity)
 {
     switch ($entity->getEntityState()) {
         case AnDomain::STATE_NEW:
             $operation = AnDomain::OPERATION_INSERT;
             $command = 'insert';
             break;
         case AnDomain::STATE_MODIFIED:
             //get all the updated serializable property/value pairs
             $operation = AnDomain::OPERATION_UPDATE;
             $command = 'update';
             break;
         case AnDomain::STATE_DELETED:
             $operation = AnDomain::OPERATION_DELETE;
             $command = 'delete';
             break;
         default:
             return;
     }
     $context = $this->getCommandContext();
     $context->operation = $operation;
     $context->entity = $entity;
     $context->data = array();
     $store = $this->getStore();
     if ($context->result = $this->getCommandChain()->run('before.' . $command, $context) !== false) {
         $context->data = $entity->getAffectedRowData();
         switch ($operation) {
             case AnDomain::OPERATION_INSERT:
                 if (!count($context->data)) {
                     throw new AnDomainRepositoryException('Attempting to store an entity with empty data');
                 }
                 $context->result = $store->insert($this, $context->data);
                 break;
             case AnDomain::OPERATION_UPDATE:
             case AnDomain::OPERATION_DELETE:
                 $keys = $this->_description->getIdentityProperty()->serialize($entity->getIdentityId());
                 $keys = array($this->_description->getIdentityProperty()->getName() => $entity->getIdentityId());
                 if ($operation & AnDomain::OPERATION_UPDATE) {
                     $context->result = count($context->data) ? $this->update($context->data, $keys) : true;
                 } else {
                     $context->result = $this->destroy($keys);
                 }
         }
         $this->getCommandChain()->run('after.' . $command, $context);
     }
     return $context->result !== false;
 }