/** * Instantiate a new entity based on the passed data This method is called from _create. * * @param string $identifier The identifier of the entity to instantiate * @param array $data The raw data * * @return AnDomainEntityAbstract */ protected function _instantiateEntity($identifier, $data) { $identifier = clone $identifier; $identifier->package = str_replace('com_', '', $data['option']); register_default(array('identifier' => $identifier, 'default' => array('ComComponentsDomainEntityComponent', 'LibComponentsDomainEntityComponent'))); return clone AnDomain::getRepository($identifier)->getClone(); }
/** * Return an array of identifiers within the component. * * @param string $class The class from which the entities are inherting * * @return array() */ public function getEntityRepositories($class) { $identifiers = $this->getEntityIdentifiers($class); foreach ($identifiers as $i => $identifier) { $identifiers[$i] = AnDomain::getRepository($identifier); } return $identifiers; }
/** * Return. * * @param KConfigInterface $config An optional KConfig object with configuration options * @param KServiceInterface $container A KServiceInterface object * * @return KServiceInstantiatable */ public static function getInstance(KConfigInterface $config, KServiceInterface $container) { if (!$container->has($config->service_identifier)) { $identifier = self::_identifier($config->service_identifier); $instance = AnDomain::getRepository($identifier, $config->toArray()); $container->set($config->service_identifier, $instance); } return $container->get($config->service_identifier); }
/** * Return an array of permissions by using the medium objects * * @return array() */ protected static function _getDefaultPermissions($component) { $identifiers = $component->getEntityIdentifiers('ComMediumDomainEntityMedium'); $permissions = array(); foreach ($identifiers as $identifier) { try { $repos = AnDomain::getRepository($identifier); if ($repos->entityInherits('ComMediumDomainEntityMedium')) { $actions = array('add'); //if commentable then allow to set //comment permissions if ($repos->hasBehavior('commentable')) { $actions[] = 'addcomment'; } $permissions[(string) $identifier] = $actions; } } catch (Exception $e) { } } return $permissions; }
/** * Return the target repository * * @return AnDomainRepositoryAbstract */ public function getTargetRepository() { return AnDomain::getRepository($this->_target); }
/** * Return the controller repository. * * @return AnDomainRepositoryAbstract */ public function getRepository() { if (!$this->_repository instanceof AnDomainRepositoryAbstract) { if (!$this->_repository instanceof KServiceIdentifier) { $this->setRepository($this->_repository); } $this->_repository = AnDomain::getRepository($this->_repository); } return $this->_repository; }
/** * Return the entity repository * * @return AnDomainRepositoryAbstract */ public function getParentRepository() { return AnDomain::getRepository($this->_parent); }
/** * Instantiate a new entity based on the passed data This method is called from _create. * * @param string $identifier The identifier of the entity to instantiate * @param array $data The raw data * * @return AnDomainEntityAbstract */ protected function _instantiateEntity($identifier, $data) { //since the identifier doesn't have an //application set, it gets the application of the parent //repository $identifier = KService::getIdentifier($identifier); $identifier->application = $this->getIdentifier()->application; return clone AnDomain::getRepository($identifier)->getClone(); }
/** * */ 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']); }
/** * Return the parent repository. * * @return AnDomainRepositoryAbstract */ public function getParentRepository() { $parent = $this->getRepository()->getDescription()->getProperty('parent'); $parent = $this->getIdentifier($parent->getParent()); return AnDomain::getRepository($parent); }
/** * Links a query to anotehr query by joining the main query resoruce. * * @param mixed $query Query object * @param string|array $condition Array condition * @param array $options Options * * @return AnDomainQuery */ public function link($query, $condition = array(), $options = array()) { if (is_string($query)) { if (strpos($query, '.') === false) { $name = $query; if ($property = $this->getRepository()->getDescription()->getProperty($name)) { $name = $property->getName(); } AnDomainQueryHelper::addRelationship($this, $name); $link = $this->getLink($name); $config = new KConfig($condition); $config->append(array('type' => $link->type, 'bind_type' => $link->bind_type, 'conditions' => array())); $link->offsetSet('type', $config->type)->offsetSet('bind_type', $config->bind_type); foreach ($config->conditions as $key => $value) { if (is_string($value)) { $value = AnDomainQueryBuilder::getInstance()->parseMethods($this, $value); } elseif ($value instanceof AnDomainResourceColumn) { $value = clone $value; } $link->conditions[$key] = $value; } return $this; } else { $query = AnDomain::getRepository($query)->getQuery(); } } elseif ($query instanceof AnDomainRepositoryAbstract) { $query = $query->getQuery(); } settype($condition, 'array'); $options = new KConfig($options); $options->append(array('type' => 'strong', 'resource' => $query->getRepository()->getResources()->main(), 'as' => $query->getRepository()->getDescription()->getEntityIdentifier()->name)); if (!isset($this->link[$options->as])) { $options->resource->setAlias(KInflector::underscore($options->as)); $destination = $query->getRepository()->getDescription()->getInheritanceColumn(); $link = array('query' => $query, 'resource' => $options->resource, 'resource_name' => $options->resource->getAlias(), 'type' => $options->type, 'conditions' => array(), 'bind_type' => $destination ? clone $destination : null); foreach ($condition as $key => $value) { $link['conditions'][$key] = $value instanceof AnDomainResourceColumn ? clone $value : $value; } $this->link[$options->as] = new KConfig($link); $this->distinct = true; } return $this; }
/** * Materialize a many-to-one relationship for the entity and the data from * the database. * * @param AnDomainEntityAbstract $instance The entity whose relationship it's materializing for * @param array $data The row data * * @return AnDomainProxyEntity */ public function materialize(array $data, $instance) { if (empty($data)) { return; } if (!$this->isMaterializable($data)) { throw new AnDomainExceptionMapping($this->getName() . ' Mapping Failed'); } $child_key = $this->_child_column->key(); //get parent value $parent_value = $data[$child_key]; //get parent class identifier //if relationsip is polymorphic then get the type from //the data if ($this->_polymorphic) { $key = $this->_type_column->key(); $parent = isset($data[$key]) ? $data[$key] : null; if ($parent) { $parent = KService::getIdentifier($parent); if (!$parent->application) { $parent->application = $this->_child->application; } } } else { $parent = $this->_parent; } //if any of the parent and parent value is missing then //nullify the relationship if (empty($parent) || empty($parent_value)) { return; } $config = array(); $config['relationship'] = $this; $config['value'] = $parent_value; $config['property'] = $this->_parent_key; $config['service_identifier'] = AnDomain::getRepository($parent)->getDescription()->getEntityIdentifier(); return new AnDomainEntityProxy(new KConfig($config)); }
/** * Get the proxied entity. Since there could many entities proxied. The getObject method will try to * load all the proxied entities of the same type in order to reduce the number of calls * to the storage later on. * * @return AnDomainEntityAbstract */ public function getObject() { //security check if (!isset($this->_object)) { $condition = array($this->_property => $this->_value); $repository = AnDomain::getRepository($this->getIdentifier()); //check if an entity exiting in the repository with $condition if ($data = $repository->find($condition, false)) { $this->_object = $data; return $this->_object; } //now time to fetch the object from the database //but lets grab all the similar entities all together $handle = $this->getIdentifier() . $this->_property; $values = isset(self::$_values[$handle]) ? self::$_values[$handle] : array(); if (empty($values)) { return; } $values = AnHelperArray::unique($values); $query = $repository->getQuery(); AnDomainQueryHelper::applyFilters($query, $this->_relationship->getQueryFilters()); $query->where(array($this->_property => $values)); $entities = $repository->fetchSet($query); //the object must have been fetched with the set //in the previous line //if the object is still not fetched, then the object //doesn't exists in the databse $this->_object = $repository->find($condition, false); if (!$this->_object) { //lets cache the null result to prevent re-fetching //the same result $query = $repository->getQuery()->where($condition)->limit(1); if ($repository->hasBehavior('cachable')) { $repository->emptyCache($query); } $this->_object = false; //if it's a required one-to-one relationship //then instantaite a new entity if the entity doesn't exists if ($this->_relationship->isOneToOne()) { if ($this->_relationship->isRequired()) { $this->_object = $repository->getEntity(array('data' => array($this->_property => $this->_value))); } } } unset(self::$_values[$handle]); } return $this->_object; }
/** * Return an array of node repositories for a list of ids. * * @param array $ids * * @return array */ public function getNodeRepositories($ids) { $types = $this->getService('repos:base.node')->getQuery()->disableChain()->id($ids); $types->distinct = true; $repositories = array(); foreach ($types->fetchValues('type') as $key => $type) { $type = explode(',', $type); $identifier = end($type); $repositories[] = AnDomain::getRepository($identifier); } return $repositories; }