現在、Flow3 (Doctrine) で "Project" と "Person" の間の最初の多対多の関係に取り組んでおり、両方のコントローラーから要素を追加、取得、および削除したいと考えています。
プロジェクト宣言:
class Project {
// ...
/**
* @var \Doctrine\Common\Collections\Collection</*...*/\Person>
* @ORM\ManyToMany(targetEntity="/*...*/\Person", mappedBy="projects")
*/
protected $persons;
// ...
}
個人宣言:
class Person {
// ...
/**
* @var \Doctrine\Common\Collections\Collection</*...*/\Project>
* @ORM\ManyToMany(targetEntity="/*...*/\Project", inversedBy="persons")
*/
protected $projects;
// ...
}
しかし、「人」(反転) 側でのみオブジェクトを追加/削除できます。少なくとも、両側からオブジェクトを取得できます。「Person」オブジェクトを使用して「Project」側で回避策を構築する必要がありますか、それとも見逃した簡単な解決策がありますか?
以下は、動作しない projectController のコード スニペットです。
public function addpersonAction() {
$param = $this->request->getArgument('project');
$project = $this->projectRepository->findByIdentifier($param['__identity']);
$selectedPersons = $this->request->getArgument('selPersons');
foreach($selectedPersons as $person)
{
if( strlen($person['__identity']) > 0 )
{
$project->addPerson($this->personRepository->findByIdentifier($person['__identity']));
}
}
$this->projectRepository->update($project);
//...
}
そして、プロジェクトの addPerson() 関数:
public function addPerson(\DS\Datenbank\Domain\Model\Person $person) {
if( !$this->persons->contains($person) )
$this->persons->add($person);
}