Add created new categories to imported tt_address set in TYPO3 8.7

276 Views Asked by At

I just wrote a news Task to import address sets from an Json file to tt_address. That works great. Now I am stuck on the problem that I am creating new categories. But I couldn't assign them to the address set.

Somebody had a clue how to do this?

$jsondivision ='JsonCategorieName' 
$address = 'AddressSet'
$categoryParent = 'PartentUID'

public function checkCategory($jsondivision, $address) {
    $extbaseObjectManager = GeneralUtility::makeInstance('TYPO3\CMS\Extbase\Object\ObjectManager');
    $this->addressRepository = $extbaseObjectManager->get('Graphodata\Gdjson2ttaddress\Domain\Repository\GdAddressRepository');
    $this->objectStorage = $extbaseObjectManager->get('TYPO3\CMS\Extbase\Persistence\ObjectStorage');
    $this->categoryRepository = $extbaseObjectManager->get('Graphodata\Gdjson2ttaddress\Domain\Repository\GdCategoryRepository');
    $newCategory = $extbaseObjectManager->get('Graphodata\Gdjson2ttaddress\Domain\Model\GdCategory');

    $newCategory->setTitle($jsondivision);
    $newCategory->setParent($categoryParent);
    $this->categoryRepository->add($newCategory);
    $address->addressRepository($newCategory);
}
1

There are 1 best solutions below

0
On

You obviously already have a Category Model (Graphodata\Gdjson2ttaddress\Domain\Model\GdCategory), so you need to set a relation in your Address model like:

/**
 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Graphodata\Gdjson2ttaddress\Domain\Model\GdCategory>
 * @lazy
 */
protected $categories;

and add getter, setter and a method to add further categories to your Address model:

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

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

/**
 * Adds a category to this categories.
 *
 * @param \Graphodata\Gdjson2ttaddress\Domain\Model\GdCategory $category
 */
public function addCategory($category)
{
    $this->getCategories()->attach($category);
}

Attention: to make this work your TCA settings have to be match some requirements. Take a look at the news extension (…/news/Configuration/TCA/tx_news_domain_model_news.php) for example to see how it works. Something like that:

…
'categories' => [
   'config' => [
      'type' => 'select',
      …
      'MM' => 'sys_category_record_mm',
      'MM_match_fields' => [
          'fieldname' => 'categories',
          'tablenames' => 'tx_gdjson2ttaddress_gdaddress',
      ],
      'MM_opposite_field' => 'items',
      'foreign_table' => 'sys_category',
      'foreign_table_where' => ' AND (sys_category.sys_language_uid = 0 OR sys_category.l10n_parent = 0) ORDER BY sys_category.sorting',
      …
   ]
]