public function __construct($type) { parent::__construct($type); $entityName = substr($type, 1); $this->repository = EntityRepository::init($entityName); $this->entityKeys = $this->repository; }
/** * Set Entity links * @param Bond\Entity\Base $entity * @param string $link String descriptor of link. Eg, ItemLinkFile * @param Bond\Entity\Container $new * * @return int The number of objects affected by this change. */ public static function set_links(Base $entity, $link, Container $new = null) { $repository = $entity->r(); if (!$repository->hasLink($link, $detail)) { throw new EntityLinkException("Bad link `{$link}`"); } if (is_null($new)) { $new = new Container(); } // check container is of the correct type $class = $new->classGet(); $classUnqualified = get_unqualified_class($class); if ($class and !in_array($classUnqualified, $detail->foreignEntities)) { $foreignEntities = implode(', ', $detail->foreignEntities); throw new EntityLinkException("Passed container is of type `{$classUnqualified}` and we need one of type(s) {$foreignEntities}"); } // Build array key'd, $existing, like so // array( // spl_obj_hash( 'link' ) => spl_obj_hash( 'entity' ) // ) $existingLinks = $repository->linksGet($entity, $link, $classUnqualified, null, false); $existing = array(); foreach ($existingLinks as $splHashLink => $link) { $existing[$splHashLink] = spl_object_hash($link[$detail->refForeign[0]]); } $newKeys = array_keys($new->collection); $newKeys = array(); $ordering = array(); $n = 0; foreach ($new as $splHashEntity => $_) { $newKeys[] = $splHashEntity; $ordering[$splHashEntity] = $n++; } // operations $toRemove = array_diff($existing, $newKeys); $toAdd = array_diff($newKeys, $existing); $unchanged = array_intersect($existing, $newKeys); // $numModified = 0; // remove while (list($splHashLink, ) = each($toRemove)) { $existingLinks[$splHashLink][$detail->refSource[1]] = null; $existingLinks[$splHashLink][$detail->refForeign[0]] = null; $numModified++; } // add $linkRepo = Repository::init($link); foreach ($toAdd as $key) { $link = $linkRepo->make(); $link[$detail->refSource[1]] = $entity; $link[$detail->refForeign[0]] = $new[$key]; if (isset($detail->sortColumn)) { $link[$detail->sortColumn] = $ordering[$key]; } $numModified++; } if (isset($detail->sortColumn)) { foreach ($unchanged as $linkKey => $entityKey) { $numModified += $existingLinks[$linkKey]->set($detail->sortColumn, $ordering[$entityKey]) ? 1 : 0; } } return $numModified; }
/** * Access to $instancesMaxAllowed * @return int * */ public function __get($key) { switch ($key) { case 'instancesMaxAllowed': return $this->{$key}; } return parent::__get($key); }