Your first model and repository

Create the files

Our package is called Events for now. The target of the package is to manage some kind of events. So the first model will be called “Event” and the corresponding repository will be called “EventRepository”. Create this to files with the following content in:

– Layh.Events/Classes/Layh/Events/Domain/Model/Event.php
– Layh.Events/Classes/Layh/Events/Domain/Repository/EventRepository.php

Event Model

The Event model represents our events that we want to add later. First of all set the properties id and title. This should be enough for the beginning.

Take a look at the annotations. In Flow annotations are more than some nice PHPDoc comments. Define the scope and tell the model that it is an entity with the first annotations above the class declaration.

Property id:
@var annotation tells Doctrine2 the data type of the property. In this case, of course, an id should be an integer. The annotations @Id and @GeneratedValue tell Flow that it is the primary key and that we use “auto increment” for this property.

Property title:
Same here as above. Data type is a string here.

Getter and Setter:
Write the getter and setter, or better just let them generate by your IDE.

Generate your model

Now you should have the following file. Perhaps there are some more comments that I removed to save some space here.

Event Repository

As you can see this is pretty much straight forward. Generate the class and extend it from the Flow repository.

Now you have functions available like:

– findAll()
– findByIdentifier() // in this case by id
– findByProperty() // replace Property by Title or Id

Generate your repository


Extend your IndexController class

Now we go ahead and add a listAction for our IndexController. The listAction is also straight forward and should not make any problems. Use the function findAll() from the repository and assign the data to your view.

Second thing here is to make the EventRepository available. Here we use annotations to inject the EventRepository. Define the data type like in the model and add the annotation @inject to make the EventRepository available in your IndexController.

 List.html template

Before we can call this action we need to add the template for the action. Like in the indexAction, where we have the Index.html as template, here we have to create the file List.html inside the folder Index.

We can just copy the Index.html file and add the for-loop like shown in the code.

Add MySql Backend to Settings.yaml

We can call the URL now, yourdomain.tld/list. There will no data because we have not saved anything until now. Also we have no MySQL Backend defined so there are two more things to do now.

Go to the document root of your web site and copy the file Settings.example.yaml to Settings.yaml. Add your database settings there.

Second thing, go to Packages/Applications/Events/Configuration and edit the Routes.yaml. Add a second route:

Use the indexAction to add some Events

For this step we will not yet create a form to add some events. So for testing we can add the events in our indexAction and delete the code after we added some events.

To do so, create a new object of type \Layh\Events\Domain\Model\Event and add it with your already existing repository to the database.

Don’t forget to delete the code in your indexAction after calling the page a couple of times, otherwise you will add a new event each time you call the indexAction.

 Calling the page

After you reloaded the index action one ore two times, you should see some events in your database and if you call <yourdomain>/list you should see the events listed.