Forms in Symfony framework. Data from multiple tables. (Lesson 13. Forms. Continued)

In the previous lesson, we learned how to create simple forms in Symfony framework that contain (and modify if necessary) data from tables. As you remember, we used models (entities) of these tables.

But in practice, we have to solve more complex tasks. For example, displaying/modifying data from multiple tables in Symfony framework.

A rough example - one table contains articles, and there is a field that sets the section id (which has a separate table). We need to make it possible to conveniently select a section for an article. Fortunately, this is a routine task for Symfony framework. =)

For this, there is a form field type - entity.

Let's modify the example from the previous lesson:

// src/Acme/TaskBundle/Controller/DefaultController.php
namespace AcmeTaskBundleController;
use SymfonyBundleFrameworkBundleControllerController;
use AcmeTaskBundleEntityModel;
use SymfonyComponentHttpFoundationRequest;
class DefaultController extends Controller
{
  public function newAction(Request $request)
   {
    $model = new Model();
    $model->setField('Some value');
    $model->setDueDate(new DateTime('tomorrow'));
    $form = $this->createFormBuilder($model)
      ->add('field', 'text')
      ->add('dueDate', 'date')
      ->add('part', 'entity', array('class' => 'HarentiusPortalBundle:Parts',
                'property' => 'name', 'label' => 'Раздел'))
      ->getForm();

if ($request->isMethod('POST')) { $form->bind($request); if ($form->isValid()) { // Here, the data processing is performed, for example, sending to the database: $em = $this->getDoctrine()->getManager(); $em->flush(); } return $this->render('AcmeTaskBundle:Default:new.html.twig', array( 'form' => $form->createView(), )); } }

Let's take a closer look at the line
  ->add('part', 'entity', array('class' => 'HarentiusPortalBundle:Parts',
                'property' => 'name', 'label' => 'Раздел'))
As we can see, the parameters array before the entity type, among other things, contains the class option, which specifies the model class, the property option, which specifies the field to display. As a result, we will get a selectbox with the ability to select a section. The sections are displayed by name. The id of the section will be entered into the page table in the database.

By default, the data is displayed as a selectbox. Well, it's not exactly by default... =)

You can choose between a Select tag (or as it was called before, a selectbox), Checkboxes, and Radio Buttons, but not in an explicit form. The choice between display types will be automatically set depending on the combination of values of the expanded and multiple options:

Input field type  expanded  multiple
select tag false false
 select tag (with multiple attribute)  false  true
 radio buttons  true  false
 checkboxes  true  true
For example,
   ->add('part', 'entity', array('class' => 'HarentiusPortalBundle:Parts',
                'property' => 'name', 'label' => 'Раздел', 'multiple' => false, 'expanded' => true))
So we will get a selection using radio buttons.