Simon Kraus
Simon Kraus

Reputation: 736

TYPO3: Extbase get related pages by sys_categories

I need to get page media and excerp of a random page that has one or more sys_categories in common with the recent page.

I tried getting the cat ids for a where clause like so:

$categories = $GLOBALS['TSFE']->page['categories'];
$cats = explode(',',$categories);
foreach($cats as $cat) {
    if($whereClauses != '') $whereClauses .= " OR ";
    $whereClauses .= $cat.' IN pages.categories';
}

But I did not get this working as the where clause of $this->pageRepository->getRecordsByField hope anyone can help me out accessing the page.

It's a 7.6.x TYPO3 and extbase

Upvotes: 1

Views: 2100

Answers (2)

René Pflamm
René Pflamm

Reputation: 3354

The categories are assigned in an MM Relation Table (sys_category_record_mm), so the categories are not saved in pages table.

So you have to query this table to get pages assigned to an category.

eg:

$GLOBALS['TYPO3_DB']->exec_SELECT_mm_query('*', 'sys_category', 'sys_category_record_mm', 'pages', 'tablenames = "pages" AND sys_category.uid = 123')

EDIT: Example of Usage with CategoryCollection:

$collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection::load($id, false, 'pages');
$items = $collection->getItems();

Upvotes: 2

cweiske
cweiske

Reputation: 31088

pages.categories only contains the number of categories the page is assigned to.

The actual category IDs are in an mm-table sys_category_record_mm.

Upvotes: 0

Related Questions