Question

Is it possible in extbase to execute two "findBy" functions at the same time.

I mean, something like this :

$newsRepository->findByCategory($category)->findByAuthor($author);

I want to get news that have a certain category, and written by a certain author.

Possible ?

Was it helpful?

Solution

The 'Magic finder methods' works only with single properties, as stated in docs you need to extend your repository (pseudo code)

public function findByCategoryAndAuthor(Tx_MyNews_Domain_Model_Category $category, Tx_MyNews_Domain_Model_Author $author){
    $query = $this->createQuery();
    $query->matching(
        $query->logicalAnd(
            $query->equals('category', $category),
            $query->equals('author', $author)
        )
    );
    return $query->execute();
}

and use:

$newsRepository->findByCategoryAndAuthor($category, $author);

OTHER TIPS

Try somethings like

In your Repository

public function initializeObject() {
    $querySettings = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings');
    // don't add the pid constraint 
    $querySettings->setRespectStoragePage(FALSE);
    $querySettings->setIncludeDeleted(FALSE);
    $querySettings->setRespectSysLanguage(FALSE);
    $this->setDefaultQuerySettings($querySettings);
}   
public function findByCategoryAndAuthor($category,$author){
    $query = $this->createQuery();
    $constraints[] = $query->logicalAnd(
        $query->equals('category', $category),
        $query->equals('author', $author)
    );
    $query->matching($constraints);
    return $query->execute();
}

In your controller

protected $yourRepository;

public function yourAction() {
    $result = $this->yourRepository->findByCategoryAndAuthor($category, $author);
    $this->view->assign('setToView', $result);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top