Pages

Sunday, July 14, 2013

Creating forms in Symfony2 part III

I had created a "one to many " relation and a form related to them. Today I am going to create a "many to many " relation and there forms. I will use these forms to create a "User loging" in my next blog post.

OK, my relationship is simple, I take 'user' and 'role'. a user may have zero or many roles and a role mayhave zero or many users. (This diagram does not shows the zero or many relationship ).



Create 'User' and 'Role' entities by executing 
$ php app/console generate:doctrine:entity
command. (keep in mind configuration format should be in "xml" format for this tutorial)

then entity configuration files should be updated. 
first open the 
<my bundle>/Resources/config/doctrine/User.orm.xml file and add fallowing to the file inside the <entity></entity> tags.

<many-to-many field="role" inversed-by="user" target-entity="Acme\<my bundle>\Entity\Role">
<join-table name="UserRole">
<join-columns>
<join-column name="userId" referenced-column-name="id" />
</join-columns>
<inverse-join-columns>
<join-column name="rolrId" referenced-column-name="id" />
</inverse-join-columns>
</join-table>
</many-to-many>

then 
open the 
<my bundle>/Resources/config/doctrine/Role.orm.xml file and add fallowing to the file inside the <entity></entity> tags.


<many-to-many field="user" mapped-by="role" target-entity="Acme\<my bundle>\Entity\User"/>


then update the entities by executing 
$ php app/console generate:doctrine:entities <my bundle>
get more information about doctrine many to many relationships from here.

update the database 
$ php app/console doctrine:schema:create or
$ php app/console doctrine:schema:update

Now entities creation is completed. Now CRUD should be created. Do these using 
$ php app/console doctrine:generate:crud

command for both User and Role entities.
Now few changes should be done to forms. For the 'Role' form actually it shouldn't have a option to select user even it was designed for it. But for the User entity it should have a  multiple select filed for select user's role's

if the CRUD creation was executed correctly file called
Acme/<my bundle>/Form/RoleType.php was generated with some php code.
inside the RoleType class there is a method named buildForm. inside this
field options is add to the $builder instance.

it is looks like that

$builder
->add('name')
->add('role')
->add('user');;

no need to display users, remove that 
->add('user');;

finally it looks like 
$builder
->add('name')
->add('role')

then open the 
Acme/<my bundle>/Form/UserType.php  file update the $builder instance inside the BuildForm method.

it is looks like

$builder
->add('username')
->add('salt')
->add('password')
->add('email')
->add('isActive')
->add('role')


role field need to be populated with roles from the "Role". to do this edit it as fallow.


$builder
->add('username')
->add('salt')
->add('password')
->add('email')
->add('isActive')
->add('role','entity',array(
'class' => 'AcmeAuthBundle:Role',
'property' => 'name',
'multiple' => TRUE
))










No comments:

Post a Comment