Symfony one to one Bidirectional embedded forms

637 views Asked by At

I have one-to-one bidirectional relationship between user and institute. Each user can own an institute at a time and each institute is owned by one user at a time. What i wanted to do is i have institutes data in my institute table, now i want to create user and during creating user, i want to assign him an institute.

I am using embedded form as collection, but the issue is the data is not loaded in embedded form.

class User extends BaseUser
{
    /**
     * @ORM\OneToOne(targetEntity="PNC\InstitutesBundle\Entity\Institutes", mappedBy="user", cascade={"persist", "merge"})
     */
    protected $institute;
}


class Institutes {
   /**
     * @ORM\OneToOne(targetEntity="Application\Sonata\UserBundle\Entity\User", inversedBy="institute")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    protected $user;
}

The UserInstituteType form.

 $builder->add('name', 'genemu_jqueryselect2_entity', array(
        //'mapped' => false,
        'required' => false,
        'class'=>'PNC\InstitutesBundle\Entity\Institutes',
        'property'=>'name',
        'multiple' => false,
        'attr' => array(
            'class' => 'form-control'
        )
        ));

and in Usertype form.

 ->add('firstname','text',array(
                'label' => ucfirst('First Name *'),
                'required' => true,
                'constraints' => $constraints['firstname'],
                'attr'=> array(
                    'class' => 'form-control',
                    'autocomplete' => 'off'
                ),
                'label_attr'=> array(
                    'class' => 'control-label',
                ),
                ))
            ->add('lastname','text',array(
                'label' => ucfirst('Last Name *'),
                'required' => true,
                'constraints' => $constraints['lastname'],
                'attr'=> array(
                    'class' => 'form-control',
                    'autocomplete' => 'off'
                )))
            ->add('username', 'text', array(
                'required' => true,
                'pattern' => '^\(\d{5}\)-\d{7}-\d{1}$',
                'label' => ucfirst('NIC *'),
                'max_length' => 15,
                'constraints' => $constraints['username'],
                'attr'=> array(
                    'class' => 'form-control',
                    'autocomplete' => 'off'
                )
            ))
      ->add('institute',new UserInstituteType(),array(
                    'data_class' => 'PNC\InstitutesBundle\Entity\Institutes',
                    'label' => false,
                    'required' => false,
                    'mapped' => false,
                ))

and in controller

public function newAction(Request $request){
   $user = new User();
        $em = $this->getDoctrine()->getManager();
        $institutes = new Institutes();
        $user->setInstitute($institutes);
        $form = $this->createForm(new UserType(), $user, array(
            'action' => $this->generateUrl('pnc_users_users_new'),
            'method' => 'POST',
        ));
}

The issue is the form is rendering well, but in institute drop down, no data is loaded so that i can pick an institute and assign it to user. New/little exp in symfony, so stuck in this, don't know where is have done wrong...

1

There are 1 answers

2
Kevin Robatel On BEST ANSWER

First, use the type entity for the institute field in the form type. When all is working, you can test again the genemu form type.

Next, I think that you have an issue with yours setter. Because of your comment:

it saves the user, but in institute table the user_id column is still empty.

class User extends BaseUser
    {
    /**
     * @ORM\OneToOne(targetEntity="PNC\InstitutesBundle\Entity\Institutes", mappedBy="user", cascade={"persist", "merge"})
     */
    protected $institute;

    public function setInstitute(Institute $institute)
    {
        // Set the owning side of the relation!
        $institute->setUser($this);
        $this->institute = $institute;
    }
}


class Institutes {
   /**
     * @ORM\OneToOne(targetEntity="Application\Sonata\UserBundle\Entity\User", inversedBy="institute")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    protected $user;

    public function setUser(User $user)
    {
        $this->user = $user;
    }
}

You need to set the owning side of the relation, if you don't no data will be saved.