Symfony : issue with PropertyAccessor

I have an issue with the propertyAccessor : There are the two following entities :

class Foo
{

    /**
     * @ORMManyToOne(targetEntity="AppEntityBar", inversedBy="foo")
     * @ORMJoinColumn(nullable=false)
     */
    private $bar;

    public function getBar(): ?Bar
    {
        return $this->bar;
    }

    public function setBar(?Bar $bar): self
    {
        $this->bar = $bar;
        return $this;
    }
}


class Bar
{
    /**
     * @ORMOneToMany(targetEntity="AppEntityFoo", mappedBy="bar", orphanRemoval=true, cascade="all")
     */
    private $foo

    public function __construct()
    {
        $this->foo = new ArrayCollection();
    }

    /**
     * @return Collection|Foo[]
     */
    public function getFoo(): Collection
    {
        return $this->foo;
    }

    public function addFoo(Foo $foo): self
    {
        if (!$this->foo->contains($foo)) {
            $this->foo[] = $foo;
            $foo->setBar($this);
        }
        return $this;
    }

    public function removeFoo(Foo $foo): self
    {
        if ($this->foo->contains($foo)) {
            $this->foo->removeElement($foo);
            // set the owning side to null (unless already changed)
            if ($foo->getBar() === $this) {
                $foo->setBar(null);
            }
        }
        return $this;
    }

}

I have plenty of Collections such as Foo, so I use the property accessor to access them programmatically. But when I do a :

            $bar = new Bar();
            $foo = new Foo();
            $propertyAccessor->getValue($bar, 'foo')->add($foo);

the $foo->bar stay at null.

Is it normal?

Will then orphean removal still work?

Thank you for your help

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *