Hi everyone, I've got a static text object and its historical revisions: StaticText 1 <→ N StaticTextHistory. So I made these relations:


     * @ORM\OneToMany(targetEntity="\Text\Entity\StaticTextHistory", mappedBy="staticText")
     * @var ArrayCollection
    protected $history;

    public function __construct()
        $this->history = new ArrayCollection();


     * @ORM\ManyToOne(targetEntity="\Text\Entity\StaticText", inversedBy="history")
     * @ORM\JoinColumn(name="static_text_id", referencedColumnName="id", onDelete="CASCADE")
     * @var \Text\Entity\StaticText
    protected $staticText;

I am trying to save StaticTextHistory object as follows:


And I always get error: Call to a member function add() on a non-object

When I dump $this->staticText->history, property history is an array, not an ArrayCollection.

Thanks a lot in advance.

EDIT: I have fixed this issue using getter for history property:


But I'm still interested what's wrong. Does it have to do anything with BaseEntity's code (starting from 240):

if ($this->$name instanceof Collection) {
                $coll = $this->$name->toArray();

                return $coll;


  • use Doctrine\Common\Collections\ArrayCollection;

Yes, it does. It's intentional. You should not let the collection “escape” the object, because you would be opening internal state of the object to the outside.

Better would be to use either magic method, that is provided by BaseEntity


or write your own method.

  • Comments:

  • Thanks for the magic

    by orech at 14.4.2015 8:20

You must first log in to participate in this discussion