sonata admin add element in one to many relationship, can not add

I think you are wrong about the "by_reference" usage - I've spend some time on the issue so I'm reporting my understanding of it. I was editing a Customer, which have a OneToMany relashionship to CustomerAddress (and this last one IS the OWNING side). So if I want to create a CustomerAddress, add it to my Customer, persist my Customer and have the whole thing properly saved there is some requirements: Set the $addresses collection as cascade persist in Doctrine Set the Customer in the CustomerAddress object when we add it to the addresses collection (because Doctrine is only looking at the owning side) This last point is done via a custom setter in Customer:
setCustomer($this);
        $this->addresses->add($address);

        return $this;
    }

    public function setAddresses($addresses)
    {
        if (gettype($addresses) == "array") {
            $addresses = new ArrayCollection($addresses);
        }

        foreach($addresses as $address)
        {
            $address->setCustomer($this);
        }

        $this->addresses = $addresses;
    }
And finally, for SonataAdminBundle to call the setters you have provided, you must set the by_reference option to false.

评论

热门帖子