0

如果我在后端设置类别,我将我的一个模型的表格设为可分类的,效果很好。如果我尝试通过前端表单添加类别,我总是会出错Call to a member function attach() on null并且不知道为什么会这样。也许你们中的任何人都可以提供帮助。

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

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

并像这样添加它

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

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

--

ext_tables.sqltx_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);
}
4

2 回答 2

2

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

public function __construct()
{
    $this->txVideoCat = new ObjectStorage();
}
于 2018-12-11T11:53:39.737 回答
0

$this->txVideoCat的是null。使用initiailizeObject()方法来分配它:

public function initializeObject()
{
  $this->txVideoCat = new ObjectStorage();
}
于 2018-12-11T11:58:43.570 回答