3

I got an extension in which i want to include some filters, i know figured out that i can filter the results that are shown of my listAction() by using findBy.

I tested it and it worked like this:

$cars = $this->carRepository->findByCarid("1");
        $this->view->assign('cars', $cars);

My problem now is i need to filter the result with more than one Parameter, what if i want to add findByColor("blue") so it gives me all cars wit hid 1 and color blue? What solution does extbase have for that kind of search queries? i can`t find anything good or understandable in the documentation.

Marcel Wasilewski
  • 2,519
  • 1
  • 17
  • 34
  • 1
    Have a look at [this](https://docs.typo3.org/typo3cms/ExtbaseFluidBook/6-Persistence/3-implement-individual-database-queries.html) - the book is a bit old, but it still works that way. – Jost Feb 17 '16 at 17:05

1 Answers1

4

You have to extend you repository and code this functionality on your own. Extbase offers you a simple but powerful API to do so.

class whatEverYourRepositoryIsCalled extends \TYPO3\CMS\Extbase\Persistence\Repository {
  public function findByFilter($carId, $color) {
    // Create empty query = select * from table
    $query = $this->createQuery();

    // Add query options
    return $query->matching(
      // ALL conditions have to be met (AND)
      $query->logicalAnd(
        // table column carId must be euqal to $carId
        $query->equals('carId', $carId),
        // table column color must be euqal to $color
        $query->equals('color', $color)
      )
    );
  }
}

This is a quite simple approach to your problem. In a real world scenario I would probably use an array of filter criteria to do the filtering like array('carId' => 1, 'color' => 'blue'). Inside of findByFilter() those values would be extracted and added to the query.

The key is to build the desired query. A quite comprehensive explanation of how to do that can be found at http://blog.typoplanet.de/2010/01/27/the-repository-and-query-object-of-extbase/. Unfortunately it's not completely up to date but the part about constructing queries is still valid.

maxhb
  • 8,554
  • 9
  • 29
  • 53