iwooli
iwooli

Reputation: 187

How to create a unique form using multiple entities fields in symfony2

I want to create a form using some fields from multiple entities. I have all the distinct entites needed already created and i am not using form classes. I need to know how to do to render a form and handle its data so i can save them to the correct tables in my database.

Here is a part of my controller in charge of doing that

public function createPublicSpaceAction() {
    //My entities
    $Room = new Room();
    $GuestList = new GuestList();
    $Guest = new Guest();

    //I need to know what to do from here

    return $this -> render('AcmeUserBundle:Default:Forms/createPublicSpace.html.twig', array());
}

I kept trying to find a solution and i came up with the idea that one form needs one entity. So maybe the solution would be to merge those entities in one so i can build the form easily. I would then have to persist data to corresponding tables. But i can't think of how to merge entities.

Upvotes: 2

Views: 2236

Answers (2)

iwooli
iwooli

Reputation: 187

I figured out a temporary solution. For those who want to know, I manually created an entity that looks like a merge of all the entity I need. This new entity has no link with Doctrine therefore it cannot create a table. Its goal is simply to allow me to build up a form and be able to manipulate data through that form. I then assign all data submitted to corresponding entities fields and persist them to the database.

Once again i know this is not the best solution. But for some reasons I won't tell, it is for me at this moment. I hope this can help some that are in the same situation than me and do not hesitate to post links that could help or better ways to do that.

Upvotes: 2

DarkLeafyGreen
DarkLeafyGreen

Reputation: 70406

It is highly recommended to use form classes http://symfony.com/doc/current/book/forms.html#creating-form-classes

They are designed to save time and make a lot of things just easier.

However to answer your question consider the following. Your action needs to handel a post request. So catch the request object with the post data:

use Symfony\Component\HttpFoundation\Request;

public function createPublicSpaceAction(Request $request)

Then get a form builder intance and create the form:

$builder = $this->createFormBuilder();

$builder->add('floor', 'text', array(
        'label' => 'Room floor',
        'data' => $room->getFloor()
));

add as much form fields as you need. There are several built-in field types: http://symfony.com/doc/current/book/forms.html#built-in-field-types

Create the form:

$form = $builder->getForm();

Pass the form to your template:

return $this -> render('AcmeUserBundle:Default:Forms/
                        createPublicSpace.html.twig', array(
    'roomForm' = $form
));

To get posted data within your action:

if ('POST' == $request->getMethod()) {
    $data = $request->request->get("form");
}

And in your template you can render the form by yourself or let twig do the job:

{{ form_widget(form.floor)}}

So this are the most importend things to mention. However you should go through http://symfony.com/doc/current/book/forms.html They actually tell you everything I wrote down.

Good luck ;)

Upvotes: 1

Related Questions