public function createModifyManyToMany(Association\ManyToMany $association, $primaryValue, array $refKeys, $action = self::ASSOC_ADD) { if ($association->getJoinKey() !== "uzivatelske-vazby") { throw new \Exception("Only custom relations can be modified!"); } if ($action === self::ASSOC_ADD) { $values = []; foreach ($refKeys as $refkey) { $values[] = ["id" => $primaryValue, "uzivatelske-vazby" => ["uzivatelska-vazba" => ["evidenceType" => $association->getTargetResource(), "object" => $refkey, "vazbaTyp" => $association->getJoinResource()]]]; } $query = $this->createInsert($association->getSourceResource(), $values); } elseif ($action === self::ASSOC_REMOVE) { throw new \Exception("Custom relation delete not implemented!"); } return $query; }
private function _manyToMany(Association\ManyToMany $association, array $primaryKeys) { $joinResult = $this->connection->select("%n,%n", $association->getJoinKey(), $association->getReferencingKey())->from("%n", $association->getJoinResource())->where("%n IN %l", $association->getJoinKey(), $primaryKeys)->fetchAssoc($association->getReferencingKey() . "," . $association->getJoinKey()); if (empty($joinResult)) { return []; } $targetResult = $this->connection->select("*")->from("%n", $association->getTargetResource())->where("%n IN %l", $association->getTargetPrimaryKey(), array_keys($joinResult))->fetchAssoc($association->getTargetPrimaryKey()); $result = []; foreach ($joinResult as $targetKey => $join) { foreach ($join as $originKey => $data) { $result[$originKey][] = $targetResult[$targetKey]; } } return $result; }