Ejemplo n.º 1
0
 /**
  * Saves relationship data to a link table containing the id's of both objects.
  *
  * This method inserts / updates the foreign objects.
  * Missing foreign objects will be removed from the link table, but not deleted.
  *
  * @param DataObjectInterface[] $objects
  * @param string $className Class name of foreign objects to load
  * @param string $linkTable Table that links two objects together
  * @param string $foreignObjectGetter Name of getter to retrieve foreign objects
  * @param string $idColumn Column on link table = the id on this object
  * @param string $foreignIdColumn Column on link table = the id on the foreign object table
  */
 public function saveManyToMany(array $objects, $className, $linkTable, $foreignObjectGetter = null, $idColumn = null, $foreignIdColumn = null)
 {
     if (empty($objects)) {
         return;
     }
     if (!$foreignObjectGetter) {
         $foreignObjectGetter = 'get' . $this->inflector->methodNameFromClass($className, true);
     }
     $foreignObjectsToSave = [];
     foreach ($objects as $object) {
         if (!method_exists($object, $foreignObjectGetter)) {
             throw new MethodNotImplementedException("{$foreignObjectGetter} must be defined on {$object->getClassName()} to save relationship");
         }
         /** @var DataObjectInterface[] $foreignObjects */
         $foreignObjects = $object->{$foreignObjectGetter}();
         if (!empty($foreignObjects)) {
             if (!is_array($foreignObjects)) {
                 throw new MethodNotImplementedException("{$foreignObjectGetter} on {$object->getClassName()} must return an array to save relationship");
             }
             foreach ($foreignObjects as $foreignObject) {
                 $foreignObjectsToSave[] = $foreignObject;
             }
         }
     }
     $this->objectMapper->unitOfWork()->executeTransaction(function () use($foreignObjectsToSave, $objects, $className, $linkTable, $foreignObjectGetter, $idColumn, $foreignIdColumn) {
         $this->objectMapper->saveAll($foreignObjectsToSave);
         $this->saveManyToManyLinks($objects, $className, $linkTable, $foreignObjectGetter, $idColumn, $foreignIdColumn);
     });
 }
Ejemplo n.º 2
0
 /**
  * Loads objects of the foreign class onto the supplied objects linked by a link table containing the id's of both objects
  *
  * @param DataObjectInterface[] $objects
  * @param string $className Class name of foreign objects to load
  * @param string $linkTable Table that links two objects together
  * @param string $idColumn Column on link table = the id on this object
  * @param string $foreignIdColumn Column on link table = the id on the foreign object table
  * @return DataObjectInterface[] Loaded objects keyed by id
  */
 public function loadManyToMany(array $objects, $className, $linkTable, $idColumn = null, $foreignIdColumn = null)
 {
     if (empty($objects)) {
         return [];
     }
     $ids = DataObject::getIds($objects);
     $queryHelper = $this->objectMapper->getQueryHelper();
     $db = $queryHelper->getConnection();
     $qb = $queryHelper->buildSelectQuery($linkTable, [$db->quoteIdentifier($idColumn) . ' AS id', $db->quoteIdentifier($foreignIdColumn) . ' AS ' . $db->quoteIdentifier('foreignId')], [$idColumn => $ids]);
     $foreignIdsById = [];
     $foreignIds = [];
     $linkRows = $qb->execute();
     $linkRows->setFetchMode(\PDO::FETCH_OBJ);
     foreach ($linkRows as $linkRow) {
         $foreignIdsById[$linkRow->id][] = $linkRow->foreignId;
         $foreignIds[$linkRow->foreignId] = true;
     }
     $foreignObjects = $this->objectMapper->findByIds($className, array_keys($foreignIds));
     unset($foreignIds);
     $foreignObjectsById = [];
     foreach ($foreignObjects as $foreignObject) {
         $foreignObjectsById[$foreignObject->getId()] = $foreignObject;
     }
     unset($foreignObjects);
     $setter = 'set' . $this->inflector->methodNameFromColumn($foreignIdColumn, true);
     foreach ($objects as $object) {
         if (method_exists($object, $setter)) {
             $foreignObjects = [];
             if (isset($foreignIdsById[$object->getId()])) {
                 $foreignIds = $foreignIdsById[$object->getId()];
                 foreach ($foreignIds as $foreignId) {
                     $foreignObjects[] = $foreignObjectsById[$foreignId];
                 }
             }
             $object->{$setter}($foreignObjects);
         } else {
             throw new MethodNotImplementedException("{$setter} must be defined on {$object->getClassName()} to load many-to-many relationship with {$className}");
         }
     }
     return $foreignObjectsById;
 }