Exemplo n.º 1
0
 /**
  * Deletes all objects by id
  *
  * @param DataObjectInterface[] $objects
  * @return int Number of db rows effected
  */
 public function deleteAll(array $objects)
 {
     if (empty($objects)) {
         return 0;
     }
     foreach ($objects as $object) {
         $this->checkArgument($object);
         $this->dispatchEvents('beforeDelete', $object);
     }
     $columns = $this->queryHelper->getDbColumns($objects[0]->getTableName());
     $ids = DataObject::getIds($objects);
     if (isset($columns['isDeleted'])) {
         $rows = $this->queryHelper->massUpdate($this->getTableName(), ['isDeleted' => 1], ['id' => $ids]);
     } else {
         $rows = $this->queryHelper->massDelete($this->getTableName(), ['id' => $ids]);
     }
     foreach ($objects as $object) {
         $object->setDeleted(true);
         $this->dispatchEvents('afterDelete', $object);
     }
     return $rows;
 }
Exemplo n.º 2
0
 public function testDeleteAll()
 {
     $objects = [];
     $object = new ExtendedDataObject();
     $objects[] = $object->setMyColumn('deleteAll 1');
     $this->repository->save($object);
     $object = new ExtendedDataObject();
     $objects[] = $object->setMyColumn('deleteAll 2');
     $this->repository->save($object);
     $rows = $this->repository->deleteAll($objects);
     $this->assertEquals(2, $rows);
     $allFromDb = $this->repository->findByIds(DataObject::getIds($objects), false);
     $this->assertCount(2, $allFromDb);
     /** @var DataObjectInterface $objectFromDb */
     foreach ($allFromDb as $objectFromDb) {
         $this->assertTrue($objectFromDb->isDeleted());
     }
 }
Exemplo n.º 3
0
 /**
  * Retrieve foreign ids for a one-to-many relationship
  *
  * @param DataObjectInterface[] $objects
  * @param string $className
  * @param string $foreignColumn
  * @return array objectId => map of foreign ids
  */
 protected function getExistingForeignIds(array $objects, $className, $foreignColumn)
 {
     $objectIds = DataObject::getIds($objects);
     $queryHelper = $this->objectMapper->getQueryHelper();
     $foreignTable = $className::getTableName();
     $foreignColumns = $queryHelper->getDbColumns($foreignTable);
     $criteria = [$foreignColumn => $objectIds];
     if (isset($foreignColumns['isDeleted'])) {
         $criteria['isDeleted'] = 0;
     }
     $qb = $queryHelper->buildSelectQuery($foreignTable, ['id', $queryHelper->getConnection()->quoteIdentifier($foreignColumn)], $criteria);
     $existingForeignObjectIds = $qb->execute()->fetchAll(\PDO::FETCH_NUM);
     $existingForeignObjectsIdsByObjectId = [];
     foreach ($existingForeignObjectIds as $row) {
         list($foreignId, $objectId) = $row;
         if (!isset($existingForeignObjectsIdsByObjectId[$objectId])) {
             $existingForeignObjectsIdsByObjectId[$objectId] = [$foreignId => true];
         } else {
             $existingForeignObjectsIdsByObjectId[$objectId][$foreignId] = true;
         }
     }
     return $existingForeignObjectsIdsByObjectId;
 }
Exemplo n.º 4
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;
 }