Example #1
0
 /**
  * If the method has a format is[A-Z] then it's a behavior name.
  *
  * @param string $method
  * @param array  $arguments
  *
  * @return mixed
  */
 public function __call($method, $arguments = array())
 {
     $parts = KInflector::explode($method);
     if ($parts[0] == 'is' && isset($parts[1])) {
         $behavior = lcfirst(substr($method, 2));
         return !is_null($this->_repository->getBehavior($behavior));
     }
     return parent::__call($method, $arguments);
 }
Example #2
0
 /**
  * 
  */
 public function unserialize($data)
 {
     $data = unserialize($data);
     $this->_repository = AnDomain::getRepository($data['identifier']);
     $this->_data = new AnDomainEntityData(new KConfig(array('entity' => $this)));
     $this->_data->setRowData($data['row']);
     $this->__service_container = $this->_repository->getService();
     $this->__service_identifier = $this->_repository->getIdentifier($data['identifier']);
 }
Example #3
0
 /**
  * Constructor.
  *
  * @param KConfig $config An optional KConfig object with configuration options.
  */
 public function __construct(KConfig $config)
 {
     $this->_initialize($config);
     if (!empty($config->aliases)) {
         foreach ($config->aliases as $alias => $property) {
             $this->setAlias($property, $alias);
         }
     }
     $this->_entity_identifier = $config->entity_identifier;
     $this->_repository = $config->repository;
     if (!$this->_repository) {
         throw new AnDomainDescriptionException('repository [AnDomainRepositoryAbstract] option is required');
     }
     if ($config->inheritance) {
         $this->_inheritance_column = $config->inheritance->column;
         //an object can only be abstract if it's
         //supports single table inheritance
         $this->_is_abstract = $config->inheritance->abstract;
         if ($config->inheritance->ignore) {
             $ignore = (array) $config->inheritance['ignore'];
             foreach ($ignore as $class) {
                 $this->_class_alias[$class] = '';
             }
         }
     }
     if (is_string($this->_inheritance_column)) {
         $this->_inheritance_column = $this->_repository->getResources()->getColumn($this->_inheritance_column);
     }
     $this->_entity_identifier = $this->_repository->getIdentifier($config->entity_identifier);
     if (!$config->identity_property) {
         $columns = $this->_repository->getResources()->main()->getColumns();
         foreach ($columns as $column) {
             if ($column->primary) {
                 $config->identity_property = KInflector::variablize($column->name);
                 break;
             }
         }
     }
     $this->_identity_property = $config->identity_property;
     //try to generate some of the propreties
     //from the database columns
     if ($config->auto_generate) {
         //if auto generate default set the identity property with the primary key
         $config->append(array('attributes' => array($this->_identity_property => array('key' => true))));
         $attributes = $config['attributes'];
         $columns = $this->_repository->getResources()->main()->getColumns();
         foreach ($columns as $column) {
             $name = KInflector::variablize($column->name);
             //merge the existing attributes
             $attributes[$name] = array_merge(array('required' => $column->required, 'column' => $column, 'type' => $column->type, 'default' => $column->default), isset($attributes[$name]) ? $attributes[$name] : array());
         }
         $config['attributes'] = $attributes;
     }
 }
Example #4
0
 /**
  * Inserts an entity into the persistant store. It will return the insertId
  *
  * @param  AnDomainRepositoryAbstract $repository
  * @param  array $data
  * @return int
  */
 public function insert($repository, $data)
 {
     $context = $this->getCommandContext();
     $context->data = $data;
     $context->repository = $repository;
     if ($this->getCommandChain()->run('before.insert', $context) !== false) {
         $vals = array();
         $this->_adapter->getConnection()->autocommit(FALSE);
         $resources = $repository->getResources();
         foreach ($data as $column => $val) {
             $column = $resources->getColumn($column);
             $key = $column->resource->getAlias();
             if (!isset($vals[$key])) {
                 $vals[$key] = array();
             }
             $vals[$key][$this->quoteName($column->name)] = $this->quoteValue($val);
         }
         $main_insert_id = null;
         foreach ($resources as $resource) {
             //check if therre now columns to be saved then probably a second empty
             //table so lets create an empty record
             $columns = isset($vals[$resource->getAlias()]) ? array_keys($vals[$resource->getAlias()]) : array();
             $values = isset($vals[$resource->getAlias()]) ? array_values($vals[$resource->getAlias()]) : array();
             if ($resource !== $resources->main()) {
                 $link = $resource->getLink();
                 $columns[] = $link->child;
                 $values[] = $main_insert_id;
             }
             $query = 'INSERT INTO ' . $this->_adapter->getTablePrefix() . $resource->getName() . '(' . implode(', ', $columns) . ') VALUES (' . implode(', ', $values) . ')';
             $this->execute($query);
             if ($resource === $resources->main()) {
                 $main_insert_id = $this->_adapter->getInsertId();
             }
         }
         $this->_adapter->getConnection()->commit();
         $this->_adapter->getConnection()->autocommit(TRUE);
         $context->result = $main_insert_id;
         $this->getCommandChain()->run('after.insert', $context);
     }
     return $context->result;
 }