10

I have ManyToMany related entities Partner and District, but when i editing relationships between object of entities changes are not saved.

Entity District

class District
{
    ...
    /**
     * @ORM\ManyToMany(targetEntity="Partner", inversedBy="districts")
     * @ORM\JoinTable(name="thedo_pcsuppurt_districts_partners",
     *     joinColumns={
     *     @ORM\JoinColumn(name="district_id", referencedColumnName="id")
     *   },
     *   inverseJoinColumns={
     *     @ORM\JoinColumn(name="partner_id", referencedColumnName="id")
     *   }
     * ) 
     */
    private $partners;
    ...

Entity Partner

class Partner
{
    ...
    /**
     * @ORM\ManyToMany(targetEntity="District", mappedBy="partners")
     * @ORM\OrderBy({"name" = "ASC"})
     */
    private $districts;
    ...

Admin class

class PartnerAdmin extends Admin
{
    public function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->with('General')
                ->add('name', null, array('label' => 'name'))
                ->add('mail', null, array('label' => 'mail'))
                ->add('ticket_prefix', null, array('label' => 'Ticket Prefix'))
            ->end()
            ->with('Districts', array('collapsed' => true))
                ->add('districts', null, array('label' => 'Districts', 'expanded' => true, 'by_reference' => false, 'multiple' => true))
            ->end()
        ;
    }
    ...

I not have any errors, but when page is reloaded relationships is not saved.

UPD In my logs i see this

[2013-10-03 10:09:19] doctrine.DEBUG: SELECT t0.id AS id1, t0.name AS name2, t0.mail AS mail3, t0.login AS login4, t0.password AS password5, t0.ticket_prefix AS ticket_prefix6, t0.created_at AS created_at7, t0.updated_at AS updated_at8 FROM thedo_pcsupport_partner t0 WHERE t0.id = ? ["1"] []
[2013-10-03 10:09:19] doctrine.DEBUG: SELECT t0.id AS id1, t0.name AS name2, t0.city_id AS city_id3 FROM thedo_pcsupport_district t0 [] []
[2013-10-03 10:09:19] doctrine.DEBUG: SELECT t0.id AS id1, t0.name AS name2, t0.city_id AS city_id3 FROM thedo_pcsupport_district t0 INNER JOIN thedo_pcsuppurt_districts_partners ON t0.id = thedo_pcsuppurt_districts_partners.district_id WHERE thedo_pcsuppurt_districts_partners.partner_id = ? ORDER BY t0.name ASC [1] []

2 Answers2

8

Can you try cascade={"persist"} on @ORM\ManyToMany annotations?

Other that that, ensure that you have your adders like so:

// District.php

public function addPartner($partner)
{
    $this->partners[] = $partner;
    $partner->setDistrict($this); // This line is important for Sonata.
}

I hope you know, that such adders (and removers) can be easily generated using doctrine:generate:entities (not doctrine:generate:entity).

TautrimasPajarskas
  • 2,686
  • 1
  • 32
  • 40
  • 1
    It should be $partner->addDistrict($this) in your example @Tautrimas Pajarskas since this is a ManyToMany example. Same for the remove method. Don't forget the orphanRemoval=true option then. – webDEVILopers Apr 14 '15 at 14:31
6

try this :

->add('AttName', 'sonata_type_model', array('multiple' => true, 'by_reference' => false)) 
  • 3
    it's actually a combination of both answers. see http://symfony.com/doc/current/cookbook/form/form_collections.html#allowing-new-tags-with-the-prototype and scroll down to the notes on "Doctrine: Cascading Relations and saving the "Inverse" side" – Heyflynn Oct 10 '14 at 18:20
  • This should be the correct answer; though honestly I did both. :) – Kyeno Mar 11 '19 at 22:10