添加自己的 sys_category 以使用 extbase 进行记录

Add own sys_category to record with extbase

我使我的一个模型的 table 可分类,如果我在后端设置类别,它就可以正常工作。如果我尝试通过前端表单添加类别,我总是会收到错误 Call to a member function attach() on null 并且不知道为什么会这样。也许你们中的任何人都可以提供帮助。

在控制器中我尝试像往常一样添加,找到类别

$videoCat = $this->categoryRepository->findByUid(28);

然后像那样添加

$this->video->addTxVideoCat($videoCat);

这就是错误发生的地方。 在下面找到我是如何将类别添加到模型中的。

--

ext_tables.sql tx_video_cat int(11) DEFAULT '0' NOT NULL,

TCA/Overrides/sys_template.php

中的扩展 tca
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::makeCategorizable(
    'addvideo',
    'tx_addvideo_domain_model_video',
    // Do not use the default field name ("categories") for pages, tt_content, sys_file_metadata, which is already used
    'tx_video_cat',
    array(
        // Set a custom label
        'label' => 'LLL:EXT:addvideo/Resources/Private/Language/locallang.xlf:video_categories',
        // This field should not be an exclude-field
        'exclude' => FALSE,
        // Override generic configuration, e.g. sort by title rather than by sorting
        // string (keyword), see TCA reference for details
        'l10n_mode' => 'exclude',
        // list of keywords, see TCA reference for details
        'l10n_display' => 'hideDiff',
    )
);

已创建扩展类别存储库

namespace Pixelink\Addvideo\Domain\Repository;

class CategoryRepository extends \TYPO3\CMS\Extbase\Domain\Repository\CategoryRepository

创建扩展模型

namespace Pixelink\Addvideo\Domain\Model;

class Category extends \TYPO3\CMS\Extbase\Domain\Model\Category
{}

打字错误的类别映射

plugin.tx_addvideo{
  persistence {
    classes{
      Pixelink\Addvideo\Domain\Model\Category {
        mapping {
          tableName = sys_category
          columns {

          }
        }
      }
    }
  }
}

并在 Model\Video.php 中添加了以下内容

/**
 * txVideoCat
 *
 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Pixelink\Addvideo\Domain\Model\Category>
 */
protected $txVideoCat = null;

/**
 * Get categories
 *
 * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Pixelink\Addvideo\Domain\Model\Category>
 */
public function getTxVideoCat()
{
    return $this->txVideoCat;
}

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

/**
 * Add category to a post
 *
 * @param \Pixelink\Addvideo\Domain\Model\Category $txVideoCat
 */
public function addTxVideoCat(\Pixelink\Addvideo\Domain\Model\Category $txVideoCat)
{
    $this->txVideoCat->attach($txVideoCat);
}

您应该在 Video 模型构造函数中初始化 属性:

public function __construct()
{
    $this->txVideoCat = new ObjectStorage();
}

你的 $this->txVideoCatnull。使用initiailizeObject()方法赋值:

public function initializeObject()
{
  $this->txVideoCat = new ObjectStorage();
}