If this tutorial is helpful to you, you can donate some money to the tutorial author via Paypal.

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 FLOW3. 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.

 * @var \Layh\Events\Domain\Model\Location
 * @ORM\ManyToOne
protected $location;
 * setter for location
 * @param \Layh\Events\Domain\Model\Location $location
 * @return void
public function setLocation($location) {
    $this->location = $location;
 * getter for location
 * @return \Layh\Events\Domain\Model\Location
public function getLocation() {
    return $this->location;

After that you should run the command ./flow3 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.
setting the usage of the namespace 

use Doctrine\ORM\Mapping as ORM;

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. 

<f:layout name="Index.html" />
<f:section name="content">
    <h1>FLOW3 Tutorial Add Event</h1>
    <f:flashMessages />
    <div id="content">
        <f:form action="create" controller="Event" package="Events" name="event" object="{event}">
            <label>Title:</label><f:form.textbox property="title" />
                        <br />
            <f:form.select property="location" name="location" options="{locations}" optionLabelField="name" />
                        <br />
            <f:form.submit value="Speichern" />

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.

$this->view->assign('locations', $this->locationRepository->findAll());

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.

<f:layout name="Index.html" />
<f:section name="content">
    <h1>FLOW3 Event listing</h1>
        <f:for each="{events}" as="event">
                {event.title} - {event.location.name}

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.

Was this tutorial helpful? So, you can donate some money to the tutorial author via Paypal.