Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get data from two different models in one controller in TYPO3 extbase

I'm novice to the "new" MVC framework extbase on TYPO3.

I'm trying to create a simple form with two selectors, one for "Schools" and one for "Programs".

I have made both models using the Extension Builder and I'm able to list all the schools and all the programs in their respective List templates.

Due the framework works with convention over configuration I don't know how to construct a Controller able to get data from those two models and pass them to the Template.

I want this code in the template works:

<f:form name="form">
        <f:form.select name="form" options="{schools}" optionValueField="uid" optionLabelField="school" />
        <f:form.select name="form" options="{programs}" optionValueField="uid" optionLabelField="program" />
    </f:form>

And my Controller skeleton:

/**
 * action show
 *
 * @param \Vendor\Extension\Domain\Model\Form $form
 * @return void
 */
public function showAction(\Vendor\Extension\Domain\Model\Form $form) {

       // Some code        
       $this->view->assign('schools', $schools);

       // Some code
       $this->view->assign('programs', $programs);

}
like image 425
Memochipan Avatar asked Dec 12 '22 08:12

Memochipan


1 Answers

All records for schools and programs can be accessed through a repository, which the Extension Builder already has created your you.

To make use of both repositories in your controller, you have to use dependency injection. Since ExtBase 4.7 you can just use the @inject annotation to inject an object by dependency injection.

Add the following to your controller:

/**
 * School repository
 *
 * @var \Vendor\Extension\Domain\Repository\SchoolRepository
 * @inject
 */
 protected $schoolRepository;

/**
 * Program repository
 *
 * @var \Vendor\Extension\Domain\Repository\ProgramRepository
 * @inject
 */
 protected $programRepository;

After you have added both repositories to you controller, you can use them in your action.

Your action then looks like this:

/**
 * action show
 *
 * @param \Vendor\Extension\Domain\Model\Form $form
 * @return void
 */
public function showAction(\Vendor\Extension\Domain\Model\Form $form) {
       $schools = $this->schoolRepository->findAll();
       $programs = $this->programRepository->findAll();

       $this->view->assign('schools', $schools);
       $this->view->assign('programs', $programs);
}

Now your form should show both select boxes with all records from the school- and programm-repository.

like image 84
derhansen Avatar answered Dec 28 '22 05:12

derhansen