Example #1
0
 private function _save($primaryValue, Adapter $joinAdapter, Adapter $targetAdapter, Entity\Collection $collection, $action = Adapter\IAdapter::ASSOC_ADD)
 {
     if ($action === Adapter\IAdapter::ASSOC_REMOVE) {
         $assocKeys = $collection->getChanges()[Entity::CHANGE_DETACH];
         foreach ($collection->getChanges()[Entity::CHANGE_REMOVE] as $targetPrimary) {
             $targetAdapter->execute($targetAdapter->createDeleteOne($this->targetReflection->getAdapterResource(), $this->targetReflection->getPrimaryProperty()->getName(true), $targetPrimary));
             $assocKeys[] = $targetPrimary;
         }
     } else {
         $assocKeys = $collection->getChanges()[Entity::CHANGE_ATTACH];
         foreach ($collection->getChanges()[Entity::CHANGE_ADD] as $entity) {
             $assocKeys[] = $targetAdapter->execute($targetAdapter->createInsert($this->targetReflection->getAdapterResource(), $entity->getData(), $this->targetReflection->getPrimaryProperty()->getName(true)));
         }
     }
     if ($assocKeys) {
         $adapterQuery = $joinAdapter->createModifyManyToMany($this, $primaryValue, array_unique($assocKeys), $action);
         $joinAdapter->execute($adapterQuery);
     }
 }