Question

I am just trying things out with extbase and ran into a problem using two tables (repostiories) and an mm_table to store the relations.

Tables are:

  • address able
  • category table
  • address_category mm table <- how to add entries in this?

I created an object of type Address and can setName etc. with no problems. But there is also a cateogry table and the two tables are related by an mm_table. And this relation I can only make in the TYPO3 backend but not in the plugin.

Code is like this:

// get repo
$addressRepo = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\GoMapsExt\Domain\Repository\AddressRepository');
$addressCategoryRepo = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\GoMapsExt\Domain\Repository\CategoryRepository');

// get category object (lead = 2))
$addressCategoryObj = $addressCategoryRepo->findByUid(2);
// attach category to address
//$go_map_address->setCategories($addressCategoryObj);   <-- need to add category here

How can I add such a relation? There is no setCategory method it seems.

Was it helpful?

Solution

first of all, check the functions inside the model. You surely have functions like addCategory(), getCategories(), removeCategory() or setCategories().

second, if really needed, do not get the repository using the makeInstance function (it is outdated), simply inject it or use the ObjectManager:

/**
 * categoryRepository
 *
 * @var \TYPO3\GoMapsExt\Domain\Repository\CategoryRepository
 * @inject
 */
protected $categoryRepository;

or/and

$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');

if (!is_object($this->categoryRepository)) {
    $this->categoryRepository = $objectManager->get('TYPO3\\GoMapsExt\\Domain\\Repository\\CategoryRepository');
}

Because Typo3 can be buggy, I usually use both.

OTHER TIPS

Using mm and other relations is very simple in extbase. First you need to configure the relation with TCA, if it works in the backend, then you already did this part. Second you need to configure the property in your model like this:

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


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

/**
 * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage
 */
public function getCategories() {
    // this prevents a fatal error if you have created your model with new instead of the objectManager
    if ($this->categories === null) {
        $this->categories = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
    }
    return $this->categories;
}

In the Controller you can add/ remove/ traverse the categories:

$go_map_address->getCategories->attach($addressCategoryObj);

foreach ($go_map_address->getCategories() as $category) {
    //$category
}

Keep in mind, that you have to save $go_map_address with the repository after adding categories to it, like with every other change to the object

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top