简体   繁体   中英

Sonata Admin embed forms one-to-many does not persist

I've been trying to implement embed forms in Sonata Admin Bundle 2.0, Sonata User Bundle 2.0 and Symfony 2.0.16 (yes, I know it's kind of old right now) and after reading a lot of forum posts and manual I could be able to implement it... but just at form level, it can't display data in edition mode, or persist it in creation mode.

Being more expecific, I'm trying to make work a relationship between an User entity (from Sonata User Bundle) with an Email entity, in an one-to-many relationship (one User has many Emails, or just one). So in the User form is gonna have one or more emails forms dynamically embeded, which seems to be working, but are disconnected with the email table.

systemUser is pointing to the table user. I got to change it because I'm using PostgreSQL and word is reserved.

UserAdmin.php

<?php

class UserAdmin extends Admin
{
    // more code

        $formMapper
            ->with('General')
                ->add('username')
                ->add('email')
                ->add('plainPassword', 'text', array('required' => false))
            ->end()
            ->with('Groups')
                ->add('groups', 'sonata_type_model', array('required' => false))
            ->end()
            ->with('Profile')
                ->add('dateOfBirth', 'date', array('required' => false))
                ->add('firstname', null, array('required' => false))
                ->add('lastname', null, array('required' => false))
                ->add('website', 'url', array('required' => false))
                ->add('locale', null, array('required' => false))
                ->add('timezone', null, array('required' => false))
            ->end()
            ->with('Emails')
                ->add('emails', 'collection', array(
                    'label' => 'Emails',
                    'required' => true,
                    'allow_add' => true,
                    'allow_delete' => true,
                    'by_reference' => false,
                ),
                array(
                    'edit' => 'inline',
                    'inline' => 'table',
                    'sortable' => 'id',
                    'targetEntity'=> 'MyProject\xBundle\Entity\Email',
                    'link_parameters' => array('context' => 'user'),
                )
            )
        ;
    // more code ...
}

User.php

<?php

class User extends BaseUser
{
    /**
     * @var array emails
     */
    protected $emails;

    /**
     * @var string user
     */
    protected $user;


    public function __construct()
    {
        $this->emails = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add emails
     *
     * @param MyProject\xBundle\Email $email
     */
    public function addEmails(\MyProject\xBundle\Entity\Email $email)
    {
        $this->emails[] = $email;
    }

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

    /**
     * Set emails
     *
     * @param  $emails
     * @return Email
     */
    public function setEmails($emails)
    {
        $this->$emails = $emails;

        foreach ($emails as $email) {
            $email->setUser($this);
        }
    }

    /**
     *
     * @param string $user
     */
    public function setUser($user)
    {
        $this->user = $user;
    }
}

Email.php

<?php

class Email
{
   /**
    * @var SystemUser
    *
    * @ORM\ManyToOne(targetEntity="User", cascade={"persist"})
    * @ORM\JoinColumns({
    *   @ORM\JoinColumn(name="system_user_id", referencedColumnName="id")
    * })
    *
    */
   private $systemUser;

   public function __construct()
   {
       $this->systemUser = new ArrayCollection();
   }

   /**
    *
    * @param MyProject\xBundle\Entity\User $systemUser
    */
   public function setSystemUser(\MyProject\xBundle\Entity\User $systemUsers = null)
   {
       $this->systemUser = $systemUser;

       return $this;
   }

   /**
    * Get systemUser
    *
    * @return MyProject\xBundle\Entity\User
    */
   public function getSystemUser()
   {
       return $this->systemUser;
   }

}

Your approach needs a 'forward' relationship from the User to the Email entity, otherwise the admin doesn't know what to change and persist. Change your code in User.php so that the email variable actually knows about its relationship. That means you should add something like

/**
 * @var array emails
 * @ORM\OneToMany(targetEntity="Email", mappedBy="systemUser", cascade={"all"})
 */
protected $emails;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM