Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extbase Repository, findBy two parameters

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 ?

like image 822
user Avatar asked Apr 01 '26 09:04

user


2 Answers

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);
like image 196
biesior Avatar answered Apr 04 '26 09:04

biesior


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);
}
like image 39
Ghanshyam Gohel Avatar answered Apr 04 '26 07:04

Ghanshyam Gohel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!