symfonyormdoctrine-ormextbasetypo3-flow

How can I add to a collection of a new obect?


i've got an object with a ManyToMany-relation to an other object via a collection in Typo3 Flow. After create a new instance of that object (which is successfully added to the repository) I can simply add to this collection.

Code snippet of abc Model:

/**
* @var \Doctrine\Common\Collections\Collection<[..]\Domain\Model\Xyz>
* @ORM\ManyToMany(targetEntity="[..]\Domain\Model\Xyz")
*/
protected $xyzs;

[...]

public function getXYZs() {
    return $this->xyzs;
}

public function addXYZ([..]\Domain\Model\Xyz $xyz) {
    if(!$this->xyzs->contains($xyz))
        $this->xyzs->add($xyz);
}

public function removeXYZ([..]\Domain\Model\Xyz $xyz) {
    if($this->xyzs->contains($xyz))
        $this->xyzs->removeElement($xyz);
}

The problem is that I can't add to this collection before I add it to the repository. (That happens because of non-existing foreign keys I guess).

Code snippet of abc controller (doesn't work!):

public function addAction([...]\$newABC)
{
    [...]

    $this->abcRepository->add($newABC);
    //the line below returns "can't use contains() / add() on a non-object"
    $newABC->addXYZ($someXYZ);

    [...]
}

The xyz collection doesn't exist in the abc controller until the addAction() is finished completely. But how can I add to this collection before the addAction() is done?


Solution

  • The final solution needs a little work arround: I take $newABC, $someXYZ and some reference stuff via a redirect to a PROTECTED! function in the same controller. (if not protected you could call it with url)

    There the persistence manager allready persisted the $newABC. So there I easily can add the $someXYZ and finaly redirect it with my reference to the place I like to go.

    Done.