将创建的新类别添加到TYPO3 8.7中设置的导入tt_address

时间:2017-12-15 09:13:05

标签: model-view-controller typo3 typo3-8.x typo3-8.7.x

我刚刚编写了一个新闻任务,用于将地址集从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);
}

1 个答案:

答案 0 :(得分:0)

您显然已经有了类别模型(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',
      …
   ]
]