Your first relation between models

It is time to connect our event to an location. Like you already expected, we can do this by using some annotations from Doctrine2 that are available in Flow. I did not yet dig that deep that I now all the annotations for Doctrine2 and I am not sure if all annotations are available. But for the beginning we don’t need very much.

Let’s start with extending our event model.

Extend the event model

First we add the new property called location. Don’t forget to set the correct type for the property, in this case it is Layh\Events\Domain\Model\Location

Second thing is to generate the getter and setter methods.

Now if you take a look at the source code below, you can see how we add the location. Each event can only have one location, but a location can have more than one event. So we add an @ManyToOne annotation. If you load the page again and check your database, you will see the foreign key of the location in your event table now.

After that you should run the command ./flow doctrine:update to update the database. And also don’t forget to add the usage of the namespace ORM at the top of your model.

Extend your template

Now we have to extend the template. I highlighted the new lines in our template below. We use the f:form.select ViewHelper for the select box. All we have to do now is to tell the ViewHelper that this select box has theĀ  property location. We told Fluid before, that the form itself handles the object event, and now it after telling that the select box is the property location, Fluid knows that this two fields belong together.

Arguments for the ViewHelper are here:

  • property: Let the form know that this select box is a property of event
  • options: This is an array containing all available locations
  • optionLabelField: This is the property of the array defined in options that should be displayed in the select box.

After we add some locations, we should see them in our form, as soon as we extended the EventController one more time.

Extend the EventController

The select box can not access the database, so what we have to get all the available locations and assign them to our view. We already injected the locationRepository so we can go ahead and just use the findAll method to get all locations.

Add the following line to your addEventAction in you EventController.

Now you should see the select box and you should be able to add locations to your event. You can extend the List.html know to display the event together with the location. I highlighted the line where I changed the template.

Now you should be able to add a location using your location form. After adding some locations, this location should be available in your addEvent action and are saved together with the event.

In the list view you should be able to see the event together with the selected location now.