Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sonata_type_model / symfony form one to many relationship not saving

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;
}
}
like image 301
Robert Dolca Avatar asked Jan 14 '23 18:01

Robert Dolca


1 Answers

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.

like image 175
Robert Dolca Avatar answered Jan 31 '23 10:01

Robert Dolca