php - 在 TYPO3 v10 上从我的控制器保存系统类别
问题描述
我有一个 IdeaModel 和一个 IdeaController。
我的模型的一个属性是“类别”(它是 db 上的正确列),我想在此列中保存系统类别的 uid。(后端表单没有问题)
我为前端用户制作了一个前端表单,以创建一个带有类别的想法。此表单将一个 IdeaModel 对象发送到我的 IdeaController
但是当我这样做时,$this->ideaRepository->add($idea);
我会收到一条错误消息Incorrect integer value: '' for column 'category' at row 1
。
是因为我的 IdeaModel 属性是 int 并且应该是 Category 类型变量吗?还是对象存储?
我一直在尝试,但我不知道如何处理 ObjectStorage?我对typo3还不太了解。如果您知道问题所在或对此有有用的指南,我将不胜感激。
IdeaModel.php
<?php
namespace Edu\EduIdeas\Domain\Model;
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
class Idea extends AbstractEntity
{
/**
* title
*
* @var string
*/
protected $title = '';
/**
* description
*
* @var string
*/
protected $description = '';
/**
* category
*
* @var int
*/
protected $category = '';
/**
* status
*
* @var int
*/
protected $status = '';
/**
* user
*
* @var int
*/
protected $user = '';
/**
* likes
*
* @var int
*/
protected $likes = '';
/**
* Sets the title
*
* @param string $title
* @return void
*/
public function setTitle(string $title): void
{
$this->title = $title;
}
/**
* Returns the title
*
* @return string $title
*/
public function getTitle(): string
{
return $this->title;
}
/**
* Sets the description
*
* @param string $description
* @return void
*/
public function setDescription(string $description): void
{
$this->description = $description;
}
/**
* Returns the description
*
* @return string $description
*/
public function getDescription(): string
{
return $this->description;
}
/**
* Sets the category
*
* @param int $category
* @return void
*/
public function setCategory(int $category): void
{
$this->category = $category;
}
/**
* Returns the category
*
* @return int $category
*/
public function getCategory(): int
{
return $this->category;
}
/**
* Sets the status
*
* @param int $status
*/
public function setStatus(int $status): void
{
$this->status = $status;
}
/**
* Returns the status
*
* @return int $status
*/
public function getStatus(): int
{
return $this->status;
}
/**
* Sets the user
*
* @param int $user
*/
public function setUser(int $user): void
{
$this->user = $user;
}
/**
* Returns the user
*
* @return int $user
*/
public function getUser(): int
{
return $this->user;
}
/**
* Sets the likes
*
* @param int $likes
*/
public function setLikes(int $likes): void
{
$this->likes = $likes;
}
/**
* Returns the likes
*
* @return int $likes
*/
public function getLikes(): int
{
return $this->likes;
}
}
IdeaController.php
<?php
namespace Edu\EduIdeas\Controller;
use TYPO3\CMS\Extbase\Annotation as Extbase;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Database\ConnectionPool;
use Edu\EduIdeas\Domain\Model\Idea;
use Edu\EduIdeas\Domain\Repository\IdeaRepository;
/***
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* (c) 2019
*
***/
/**
* IdeasController
*/
class IdeasController extends ActionController
{
/**
* Inject the idea repository
*
* @param Edu\EduIdeas\Domain\Repository\IdeaRepository $ideaRepository
*/
private $ideaRepository;
public function injectIdeaRepository(IdeaRepository $ideaRepository)
{
$this->ideaRepository = $ideaRepository;
}
/**
* action list
*
* @return string An HTML with a list of posted aideas
*/
public function listAction()
{
$ideas = $this->ideaRepository->findAll();
$this->view->assign('ideas', $ideas);
}
/**
* @param \Edu\EduIdeas\Domain\Model\Idea $idea The new Idea object
* @return string An HTML form for creating a new idea
* @Extbase\IgnoreValidation("newIdea")
*/
public function newAction(Idea $newIdea = null)
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tt_content');
$categories = $queryBuilder->select('title', 'uid')->from('sys_category')->where($queryBuilder->expr()->eq('parent', $queryBuilder->createNamedParameter(7)))
->execute()->fetchAll();
$this->view->assign('newIdea', $newIdea);
$this->view->assign('categories', $categories);
}
/**
* Creates a new Idea
*
* @param Idea $idea The idea to save on db
* @return void
*/
public function createAction(Idea $idea)
{
// TODO access protection
$idea->setStatus(1);
$idea->setUser($GLOBALS['TSFE']->fe_user->user['uid']);
$this->ideaRepository->add($idea);
$this->redirect('list');
}
}
'category' => [
'label' => 'LLL:EXT:hebo_ideas/Resources/Private/Language/locallang_db.xlf:hk_ideas_idea.category',
'config' => [
'type' => 'select',
'renderType' => 'selectTree',
'treeConfig' => [
'parentField' => 'parent',
'rootUid' => 7,
'appearance' => [
'showHeader' => false,
'expandAll' => true,
],
],
'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',
'size' => 10,
'minitems' => 0,
'maxitems' => 1,
]
],
PD:这是一个整数
解决方案
该错误意味着您正在尝试将空字符串插入整数列。如何解决取决于表结构。如果该列可以为空,请将默认值设置为null
:
protected $category = null;
或者,如果该列具有其他一些默认值,例如0
,您可以将其设置为该值。否则,您应该在插入之前显式提供一个有效的整数值:
$idea->setCategory($categoryId);
$this->ideaRepository->add($idea);
推荐阅读
- c# - UWP 数据绑定对象到 UserControl
- java - 如何识别用户是否是首次在 Alexa 中启动技能?
- javascript - react 组件似乎在我的 keyup 事件上重新渲染
- reactjs - Rails5 的 Dockerfile 和 docker-compose 配置为 RESTful API、nodeJs 和 React.js
- spring - @Profile 不适用于消息传递网关
- python - flask-oidc - 需要令牌但无效 - 使用 google sso
- spring-security - 会话认证策略的 Spring Security 集成测试
- python - 以相同的方式在python中随机拆分两个列表
- java - Apache Ant:从同一个 build.xml 运行 2 个 java 程序
- slider - JSSOR 错误 - 移动 Safari 方向更改时的全宽缩放