Example #1
0
 /**
  * Persists relocations to database.
  * @param SortableAdapter $ea
  */
 private function persistRelocations(SortableAdapter $ea)
 {
     if (!$this->persistenceNeeded) {
         return;
     }
     $em = $ea->getObjectManager();
     foreach ($this->relocations as $hash => $relocation) {
         $config = $this->getConfiguration($em, $relocation['name']);
         foreach ($relocation['deltas'] as $delta) {
             if ($delta['start'] > $this->maxPositions[$hash] || $delta['delta'] == 0) {
                 continue;
             }
             $ea->updatePositions($relocation, $delta, $config);
         }
     }
     $this->persistenceNeeded = false;
 }
 /**
  * @param SortableAdapter $ea
  */
 private function processRelocations(SortableAdapter $ea)
 {
     $em = $ea->getObjectManager();
     foreach ($this->relocations as $hash => $relocation) {
         $config = $this->getConfiguration($em, $relocation['name']);
         foreach ($relocation['deltas'] as $delta) {
             if ($delta['start'] > $this->maxPositions[$hash] || $delta['delta'] == 0) {
                 continue;
             }
             $ea->updatePositions($relocation, $delta, $config);
             $meta = $em->getClassMetadata($relocation['name']);
             // now walk through the unit of work in memory objects and sync those
             $uow = $em->getUnitOfWork();
             foreach ($uow->getIdentityMap() as $className => $objects) {
                 // for inheritance mapped classes, only root is always in the identity map
                 if ($className !== $ea->getRootObjectClass($meta) || !$this->getConfiguration($em, $className)) {
                     continue;
                 }
                 foreach ($objects as $object) {
                     if ($object instanceof Proxy && !$object->__isInitialized__) {
                         continue;
                     }
                     // if the entity's position is already changed, stop now
                     if (array_key_exists($config['position'], $ea->getObjectChangeSet($uow, $object))) {
                         continue;
                     }
                     $oid = spl_object_hash($object);
                     $pos = $meta->getReflectionProperty($config['position'])->getValue($object);
                     $matches = $pos >= $delta['start'];
                     $matches = $matches && ($delta['stop'] <= 0 || $pos < $delta['stop']);
                     $value = reset($relocation['groups']);
                     while ($matches && ($group = key($relocation['groups']))) {
                         $gr = $meta->getReflectionProperty($group)->getValue($object);
                         if (null === $value) {
                             $matches = $gr === null;
                         } else {
                             $matches = $gr === $value;
                         }
                         $value = next($relocation['groups']);
                     }
                     if ($matches) {
                         $meta->getReflectionProperty($config['position'])->setValue($object, $pos + $delta['delta']);
                         $ea->setOriginalObjectProperty($uow, $oid, $config['position'], $pos + $delta['delta']);
                     }
                 }
             }
         }
     }
     // Clear relocations
     $this->relocations = array();
     $this->maxPositions = array();
 }