-->

Controllers and Template (how to filter results co

2019-08-31 14:06发布

问题:

I got following problem on my hand. My Site has a extension (written by me) which modelates a vehicle park. There are vehicles (german: Fahrzeug sry about that and in the future when I use german in my code/post) and there are VCategories (FzKategorie). They stand in N-1 relation , meaning N vehicles have all 1 Category min/max. I created a second plugin for the task, and gave it the default action: ListByCateory. Now I'm stuck WHEN to filter my restults, WHERE to hand over arguments which Category items should be shown and HOW to make sense of controllers and Fluid Templates in general. I read up on the whole (outdated and missing //code) extension developiong documentation and I can't get further, yet.

<?php
namespace Y7group\Y7Fahrzeugdatenbank\Controller;
/**
* FahrzeugController
*/
class FahrzeugController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
    /**
     * fahrzeugRepository
     *
     * @var \Y7group\Y7Fahrzeugdatenbank\Domain\Repository\FahrzeugRepository
     * @inject
     */
    protected $fahrzeugRepository = NULL;
    [...]
    /**
     * action listByCategory
     *
     * @return void
     */
    public function listByCategoryAction(){//\Y7group\Y7Fahrzeugdatenbank\Domain\Model\FzKategorie $cat) {
        $vehicles = $this->fahrzeugRepository->findAll(); // get all vehicles
        $this->view->assign('category', 1); // here assign another variable like $cat.
        $this->view->assign('vehicles', $vehicles); // hand over the query results
    }
}

And here my template:

<f:layout name="Default" />
<f:section name="main">
<h1>Alle Fahrzeuge Category</h1>
<f:flashMessages renderMode="div" />
<table  class="tx_y7fahrzeugdatenbank" >
<tr>
    <th><f:translate key="tx_y7fahrzeugdatenbank_domain_model_fahrzeug.name" /></th>
    <th><f:translate key="tx_y7fahrzeugdatenbank_domain_model_fahrzeug.beschreibung" /></th>
    <th><f:translate key="tx_y7fahrzeugdatenbank_domain_model_fahrzeug.bild" /></th>
    <th><f:translate key="tx_y7fahrzeugdatenbank_domain_model_fahrzeug.datenblatt" /></th>
</tr>

<f:for each="{vehicles}" as="fahrzeug">
<f:if condition="{category}=={fahrzeug.category}">
    <tr>
        <td><f:link.action action="show" arguments="{fahrzeug : fahrzeug}"> {fahrzeug.name}</f:link.action></td>
        <td><f:link.action action="show" arguments="{fahrzeug : fahrzeug}"> {fahrzeug.beschreibung}</f:link.action></td>
        <td><f:image src="{fahrzeug.bild.originalResource.originalFile.uid}" alt="{fahrzeug.beschreibung}" width="300"/></td>
        <td><f:link.action action="show" arguments="{fahrzeug : fahrzeug}"> {fahrzeug.datenblatt.originalResource.name}</f:link.action></td>
    </tr>
</f:if>
</f:for>

Who get's my point and who is able to help me? I have do get this working till tomorrow. :_(

回答1:

How about just getting the vehicles from the DB that matches your category? If you set up your TCA properly, instead of

$vehicles = $this->fahrzeugRepository->findAll();

try

$vehicles = $this->fahrzeugRepository->findByCategory($category);

You most likely want to make the $category configurable. Let me know, if you need help with that.