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

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/Domain/Model/Event.php
- Layh.Events/Classes/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 FLOW3 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 FLOW3 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

Generating a model 

./flow3 kickstart:model Layh.Events Event title:string

Now you should have the following file. Perhaps there are some more comments that I removed to save some space here.
Layh.Events/Classes/Domain/Model/Event.php 

namespace Layh\Events\Domain\Model;
 
use TYPO3\FLOW3\Annotations as FLOW3;
 
/**
 * A Event
 *
 * @FLOW3\Scope("prototype")
 * @FLOW3\Entity
 */
class Event {
 
    /**
     * The title
     * @var string
     */
    protected $title;
 
 
    /**
     * Get the Event's title
     *
     * @return string The Event's title
     */
    public function getTitle() {
        return $this->title;
    }
 
    /**
     * Sets this Event's title
     *
     * @param string $title The Event's title
     * @return void
     */
    public function setTitle($title) {
        $this->title = $title;
    }
 
}

Event Repository

As you can see this is pretty much straight forward. Generate the class and extend it from the FLOW3 repository.
Now you have functions available like:

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

Generate your repository

Generate your repository 

./flow3 kickstart:repository Layh.Events Event

Layh.Events/Classes/Domain/Repository/EventRepository.php  

namespace Layh\Events\Domain\Repository;
 
/*                                                                        *
 * This script belongs to the FLOW3 package "Layh.Events".                *
 *                                                                        *
 *                                                                        */
 
use TYPO3\FLOW3\Annotations as FLOW3;
 
/**
 * A repository for Events
 *
 * @FLOW3\Scope("singleton")
 */
class EventRepository extends \TYPO3\FLOW3\Persistence\Repository {
 
    // add customized methods here
 
}

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.
Layh.Events/Classes/Controller/IndexController.php 

/**
* @var Layh\Events\Domain\Repository\EventRepository
* @FLOW3\Inject
*/
protected $eventRepository;
 
/**
 * @return void
 */
public function listAction() {
    $eventList = $this->eventRepository->findAll();
    $this->view->assign('events', $eventList);
}

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.
Layh.Events/Resources/Private/Templates/Index/List.html 

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

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:

Layh.Events/Configuration/Routes.yaml 

-
    name: 'Action route'
    uriPattern: '{@action}'
    defaults:
      @package: Layh.Events
      @controller: Index
      @format: 'html'
 
-
    name: 'Homepage'
    uriPattern: ''
    defaults:
      @package: Layh.Events
      @controller: Index
      @format: 'html'
      @action: 'index'

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 \F3\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.

Layh.Events/Classes/Controller/IndexController.php 

public function indexAction() {
    $event = new \Layh\Events\Domain\Model\Event();
    $event->setTitle('My Event Title');
    $this->eventRepository->add($event);
}

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.

 


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