简体   繁体   中英

How to write a query for sys_categories in TYPO3

I've written a small extension in extbase/fluid and now I'm facing a little problem in the repository.

With my extension you can create item sthat are using system_categories for categorization (similiar to every news extension). What I want to do is to show all items with category x on page X and all of category y on Page Y.

I know that I have to write the query in my itemRepository.php but I can't get it working, so here' my code of the repository:

 public function findSearchForm($limit)
{

    $query = $this->createQuery();

    $query->getQuerySettings()->setLanguageUid($GLOBALS["TSFE"]->tmpl->setup['config.']['sys_language_uid']);
    $query->matching(
        $query->like('title','%'.$search.'%')
    );

   # $query->setOrderings(array('title' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING));


    return $query->execute();
}

I tried to extend the query after the "query->like" even with a statement like this

$query->statement("SELECT * FROM sys_category_record_mm WHERE uid_local = '11'")

First of all: How did you enable categorization on your records? Does it correctly work in the backend?

When your TCA is correctly configured for category usage, you can use common repository methods to filter the result. But you have to combine the condition with your "title"-condition using AND.

Try something like this:

public function findSearchForm($limit)
{

    $query = $this->createQuery();
    $constraints = array();
    $yourCategory = 11;

    // Your $search-variable seems to be undefined?
    //$constraints[] = $query->like('title','%'.$search.'%');
    $constraints[] = $query->contains('categories', $yourCategory);

    $query->matching(
        $query->logicalAnd($constraints)
    );

    // I think you dont need this...
    $query->getQuerySettings()->setLanguageUid($GLOBALS["TSFE"]->tmpl->setup['config.']['sys_language_uid']);

    return $query->execute();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM