php - Typo3 - 如何在加入集合中添加自定义字段?
问题描述
问题
有一个扩展,我们命名为 ProjectExams,它显示了考试列表。有一个包含 Controller、Model、ModelRepository、Tables 和 Template 的结构。一切都使用 Extbase 检索数据集合(延迟加载模型绑定)。
我们有两个请求:
* 绕过绑定并仅使用一个“原始” SQL 查询检索集合,并在 DB 中连接;
* 在 View(Template) 中返回一个没有对象元素的数组
背景和代码
模型集合代表考试列表。除了 UID、Title、Description 和 MySQL 表“domain_model_exams”中的其他字段外,模板文件还必须显示每次考试的位置(城市)。然后 ModelRepository 使用一个方法“findExamsByDate”,由控制器调用,它被请求实现这个单一的“原始”SQL 查询。
控制器代码
public function listAction($success = null, $message = '')
{
// here some code, not related to the issue
$examStartFrom = $_POST['exams']['examStartFrom'] ?? null;
$examStartTo = $_POST['exams']['examStartTo'] ?? null;
$dateBoundary = new DateBoundary($examStartFrom, $examStartTo);
$isAdmin = ($role === User::USER_TYPES_EXAMINER_ADMIN && $dateBoundary->isValid());
$exams = $this->examRepository->findExamsByDate(
$dateBoundary->getFromDate(),
$dateBoundary->getToDate(),
$isAdmin
);
// here some code, not related to the issue
$this->view->assign('exams', $exams);
// here some code, not related to the issue
}
模板(视图)文件需要一个变量exams.city
在模型文件中,我们有一个$city
带有 set 和 get 函数的 protected var
模型库代码
/**
* Get all exams within a certain time frame
*
* @param \DateTime $from
* @param \DateTime $to
* @param bool $isAdmin
*
* @return QueryResultInterface|array|Exam[]
*
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
*/
public function findExamsByDate(\DateTime $from, \DateTime $to, bool $isAdmin = false)
{
$whereConditions = [];
$query = $this->createQuery();
// here some lines with where conditions related to time frame
if ($from instanceof \DateTime) {
$whereConditions[] = 's.course_start >= ' . $from->getTimestamp();
}
if ($to instanceof \DateTime) {
$whereConditions[] = 's.course_start <= ' . $to->getTimestamp();
}
$query->statement(sprintf('
SELECT s.*, a.place as city
FROM domain_model_exams AS s
LEFT JOIN domain_model_address AS a ON s.address_id = a.uid
WHERE %1$s
ORDER BY s.course_start ASC
', implode(' AND ', $whereConditions)));
return $query->execute();
}
结果
使用\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($exams);
我们得到这个对象:
TYPO3\CMS\Extbase\Persistence\Generic\QueryResultprototypeobject (762 items)
0 => Domain\Model\Examprototypepersistent entity (uid=5, pid=568)
title => protected 'The mighty title for this exam' (xx chars)
descriptionText => protected '' (0 chars)
courseStart => protectedDateTimeprototypeobject (2014-12-05T09:00:00+00:00, 1417770000)
courseEnd => protectedDateTimeprototypeobject (2014-12-07T11:30:00+00:00, 1417951800)
.
. other fields
.
city => protected NULL
.
. other fields
.
我们的期望是city => protected 'Frankfurt am Main' (17 chars)
没有NULL
,因为我们在对数据库运行 SQL 查询时得到这个。
解决方案
解决方案
- 从模型中删除与该字段“城市”相关的属性和get-set函数;在我们的具体案例中:
/**
* @var \Domain\Repository\AddressRepository
*/
- Typo3扩展用于有一个特定的配置文件,用于自定义模型配置,例如添加一个新的自定义字段 /Exams/Configuration/ TCA /domain_model_exam.php 在这个文件中,在返回数组中,我们必须添加一个新元素,其中包含特定的这个新字段的配置:
return call_user_func(function ($_EXTKEY) {
// some other settings
return [
'ctrl' => [
// other fields
'city' => 'city',
// more fields
],
'interface' => [
'showRecordFieldList' => 'field_a, field_b, city, field_n',
],
'types' => [
'1' => ['showitem' => 'field_a, field_b, city, field_n'],
],
'columns' => [
'city' => [
'exclude' => 1,
'label' => 'LLL:EXT:exams/Resources/Private/Language/locallang_db.xlf:domain_model_exam.city',
'config' => [
'type' => 'input',
'size' => 30,
'eval' => 'trim'
],
],
];
}, 'seminars');
对于这些特定请求的任何其他更好的解决方案,我们将不胜感激。
推荐阅读
- mongodb - MongoDB查询以查找嵌套数组中的列表
- asp.net - 电子邮件确认后如何将用户重定向到主页?
- amazon-web-services - 使用 aws java sdk 版本 2 从资源文件夹中读取 aws 配置和凭据
- database - 布尔表达式不能为空?
- asp.net - 通过 Microsoft Graph 访问共享邮箱 - “访问被拒绝”
- java - C#如何关闭所有子进程
- javascript - firebase.firestore() .set() 没有第一次触发
- javascript - 如何在节点 js 中记录特定的错误消息?
- javascript - Vue.js:访问另一个组件中的数组数据
- c# - 如何返回 IEnumerable
/IL 列表 为 null 或使用 null-coalescing 运算符的值