我在前端有一个表单,用于创建模型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批注。例如,请参见showAction
:https://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批注后,清除系统缓存。