TYPO3 扩展 - 属性 映射程序无法识别外部域 class

TYPO3 extension - foreign domain class cannot be identified by property mapper

我在前端有一个创建模型对象的表单 Question。每个问题需要分配一个Category,在表格中选择(通过<f:form.select>)然后提交,这样:

tx_qa_questionform[question][category] = 1

基本上提交 Category 的 UID。提交表格后我得到一个例外:

#1297759968 TYPO3\CMS\Extbase\Property\Exception
Exception while property mapping at property path "category": Could not find a suitable type converter for "Category" because no such class or interface exists.

因此,PropertyMapper 似乎无法识别域 Category,尽管它是在扩展中定义的。这不应该自己发生吗?还是我错过了任何配置?

Model/Question.php

namespace Vendor\QA\Domain\Model;

class Question extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity {

    /**
     * @var Category $category
     */
    protected $category;

    /**
     * @var string $name
     */
    protected $name = '';

    /**
     * @var string $mail
     */
    protected $mail = '';

    /**
     * @var string $content
     */
    protected $content = '';

    public function __construct(Category $category, string $name, string $mail, string $content) {
        $this->setCategory($category);
        $this->setName($name);
        $this->setMail($mail);
        $this->setContent($content);
    }

    public function setCategory(Category $category): void {
        $this->category = $category;
    }

    public function getCategory(): Category {
        return $this->category;
    }

    public function setName(string $name): void {
        $this->name = $name;
    }

    public function getName(): string {
        return $this->name;
    }

    public function setMail(string $mail): void {
        $this->mail = $mail;
    }

    public function getMail(): string {
        return $this->mail;
    }

    public function setContent(string $content): void {
        $this->content = $content;
    }

    public function getContent(): string {
        return $this->content;
    }
}

Model/Category.php

namespace Vendor\QA\Domain\Model;

class Category extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity {

    /**
     * @var string $name
     */
    protected $name;

    public function __construct(string $name) {
        $this->setName($name);
    }

    public function setName(string $name): void {
        $this->name = $name;
    }

    public function getName(): string {
        return $this->name;
    }
}

Controller/FrontendController.php

namespace Vendor\QA\Controller;

use Vendor\QA\Domain\Model\Question;
use Vendor\QA\Domain\Repository\QuestionRepository;
use Vendor\QA\Domain\Repository\CategoryRepository;

class FrontendController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {

    private $questionRepository;
    private $categoryRepository;

    public function injectQuestionRepository(QuestionRepository $questionRepository) {
        $this->questionRepository = $questionRepository;
    }

    public function injectCategoryRepository(CategoryRepository $categoryRepository) {
        $this->categoryRepository = $categoryRepository;
    }

    public function formAction() {
        $categories = $this->categoryRepository->findAll();
        $this->view->assign('categories', $categories);
        $this->view->assign('newQuestion', null);
    }

    public function createQuestionAction(Question $question) {
        $this->questionRepository->add($question);
    }
}

您的控制器操作函数中需要@phpdoc 注释。例如,请参见此处的 showActionhttps://docs.typo3.org/m/typo3/book-extbasefluid/master/en-us/7-Controllers/1-Creating-Controllers-and-Actions.html#flow-pattern-display-a-single-domain-object

更改 phpdoc 注释后,清除系统缓存。