Skip to content

Instantly share code, notes, and snippets.

@overcome
Forked from iamandrewluca/MyModel.php
Last active August 23, 2017 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save overcome/2f8fab0dae95a4aa19303c0490a4d869 to your computer and use it in GitHub Desktop.
Save overcome/2f8fab0dae95a4aa19303c0490a4d869 to your computer and use it in GitHub Desktop.
typo3 extbase categories
<?php
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::makeCategorizable(
$_EXTKEY,
'tx_mymodels_domain_model_mymodel'
);
#
# Table structure for table 'tx_mymodels_domain_model_mymodel'
#
CREATE TABLE tx_mymodels_domain_model_mymodel (
categories int(11) unsigned DEFAULT '0' NOT NULL,
);

To use Extbase CategoryRepository, to work with Categories on our model we need to:

Add in ext_tables.sql, in our model, the categories field categories int(11) unsigned DEFAULT '0' NOT NULL, Make your model categorizable, by adding bellow snipet in ext_tables.php

\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::makeCategorizable(
    $_EXTKEY,
    'tx_mymodels_domain_model_mymodel'
);

Create own Category model and CategoryRepository by extending extabase Category and CategoryRepository

namespace MyVendor\MyModels\Domain\Model;
class Category extends \TYPO3\CMS\Extbase\Domain\Model\Category {}
namespace MyVendor\MyModels\Domain\Repository;
class CategoryRepository extends \TYPO3\CMS\Extbase\Domain\Repository\CategoryRepository {}

In your MyModelController inject CategoryRepository

/**
 * CategoryRepository
 *
 * @var \MyVendor\MyModels\Domain\Repository\CategoryRepository
 * @inject
 */
protected $categoryRepository = null;

Add to your model categories field with getters and setters

/**
 * categories
 *
 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\MyVendor\MyModels\Domain\Model\Category>
 * @lazy
 */
protected $categories;

/**
 * Get categories
 *
 * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\MyVendor\MyModels\Domain\Model\Category>
 */
public function getCategories()
{
    return $this->categories;
}

/**
 * Set categories
 *
 * @param  \TYPO3\CMS\Extbase\Persistence\ObjectStorage $categories
 * @return void
 */
public function setCategories($categories)
{
    $this->categories = $categories;
}

In Configuration\TypoScript\setup.txt map back your model to extbase category table

plugin.tx_mymodels.persistence.classes {
	MyVendor\MyModels\Domain\Model\Category {
		mapping {
			recordType = 0
			tableName = sys_category
		}
	}
}

Use this way:

// gets all categories, all across the site
$categoryRepository->findAll();

// gets categories for this model
$myModel->getCategories();

// get all models for specific category
$collection = \TYPO3\CMS\Frontend\Category\Collection\CategoryCollection::load(
        $categoryId,
        TRUE,
        'tx_mymodels_domain_model_mymodel',
        'categories'
);

If you want to take categories records only from a sys folder you need to configure repository:

defaultQuerySettings->setRespectStoragePage(true)
defaultQuerySettings->setStoragePageIds($storagePageIds); 
<?php
/**
* categories
*
* \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\Category>
* @lazy
*/
protected $categories;
/**
* MyModel constructor.
*/
function __construct()
{
$this->categories = new ObjectStorage();
}
/**
* Get categories
*
* @return ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\Category>
*/
public function getCategories()
{
return $this->categories;
}
/**
* Set categories
*
* @param ObjectStorage $categories
* @return void
*/
public function setCategories($categories)
{
$this->categories = $categories;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment