sonata_type_model / symfony form one to many relationship not saving

Robert Dolca picture Robert Dolca · Dec 15, 2012 · Viewed 8.2k times · Source

I have a one to many relationship between accounts and users. I am using sonata admin for CRUD. As I know, it uses Symfony forms to get the job done. I tried to add a field like this:

        ->add('users', 'entity', array(
            'class'    => 'AcmeDemoBundle:User',
            'required' => true,
            'multiple' => true,
            'expanded' => true
        ));

I also tried with sonata_type_model

        ->add('users', 'sonata_type_model', array(
            'class'    => 'AcmeDemoBundle:User',
            'required' => true,
            'multiple' => true,
            'expanded' => true
        ));

The issue is that nothing (related to the relationship) gets saved when the account is edited (when I edit a user everything gets updated). I know that I have to update the owning side (user side) but I did not succeed doing that because "public function addUser($user)" is not called so I can not add something like "$user->setAccount($this)" to that function.

My entities look like this:

class Account
{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\Column(type="string")
 */
protected $name;

/**
 * @ORM\OneToMany(targetEntity="Acme\DemoBundle\User", mappedBy="account", cascade={"persist", "remove"})
 */
protected $users;

/**
 * Add users
 *
 * @param Acme\DemoBundle\Entity\User $user
 * @return Account
 */
public function addUser(\Acme\DemoBundle\Entity\User $user)
{
    $user->setAccount($this);
    $this->users[] = $user;

    return $this;
}

/**
 * Remove users
 *
 * @param Acme\DemoBundle\Entity\User $user
 */
public function removeUser(\Acme\DemoBundle\Entity\User $user)
{
    $user->setAccount(null);
    $this->users->removeElement($user);
}

/**
 * Get users
 *
 * @return Doctrine\Common\Collections\Collection 
 */
public function getUsers()
{
    return $this->users;
}
}

.

class User extends BaseUser
{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\ManyToOne(targetEntity="Acme\DemoBundle\Account", inversedBy="users")
 */
protected $account;

/**
 * Set account
 *
 * @param Acme\DemoBundle\Entity\Account $account
 * @return User
 */
public function setAccount(\Acme\DemoBundle\Entity\Account $account = null)
{
    $this->account = $account;

    return $this;
}

/**
 * Get account
 *
 * @return Acme\DemoBundle\Entity\Account 
 */
public function getAccount()
{
    return $this->account;
}
}

Answer

Robert Dolca picture Robert Dolca · Dec 17, 2012

My issue was related to this https://github.com/symfony/symfony/issues/1540

After I added by_reference => false Account setters are called and the owning side can be updated.