我刚刚编写了一个新闻任务,用于将地址集从Json文件导入到tt_address。这很好用。现在我陷入了创建新类别的问题。但我无法将它们分配给地址集。
有人知道如何做到这一点?
$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);
}
你显然已经有了一个分类模型(Graphodata\Gdjson2ttaddress\Domain\Model\GdCategory
),所以你需要在你的地址模型中设置一个关系,如:
/**
* @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Graphodata\Gdjson2ttaddress\Domain\Model\GdCategory>
* @lazy
*/
protected $categories;
并添加getter,setter和方法以向Address模型添加更多类别:
/**
* 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);
}
注意:要完成这项工作,您的TCA设置必须符合某些要求。看看news
扩展(... / news / Configuration / TCA / tx_news_domain_model_news.php),看看它是如何工作的。像这样的东西:
…
'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',
…
]
]