0

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

1 回答 1

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',
      …
   ]
]
于 2017-12-15T09:39:24.547 回答