有一个扩展,我们命名为ProjectExams,它显示考试列表。有一个包含Controller,Model,ModelRepository,Tables和Template的结构。一切都可以使用Extbase来检索数据集合(延迟加载模型绑定)。
我们有两个要求:
*绕过绑定并仅使用一个“原始” SQL查询(在数据库中具有联接)来检索集合;
*在View(Template)中返回一个没有对象元素的数组
模型收藏代表考试列表。除了UID,标题,描述和MySQL表“ domain_model_exams”中的其余字段外,模板文件还必须显示每次考试的位置(城市)。然后,ModelRepository使用具有方法“ findExamsByDate”的方法,该方法由Controller调用,在该方法中,它被要求实现此单个“原始” 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
在 Model 文件中,我们有一个受保护的var $city
,其中包含set和get函数
ModelRepository 代码
/**
* 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查询时得到的。
答案 0 :(得分:1)
/**
* @var \Domain\Repository\AddressRepository
*/
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');
对于这些特定要求的任何其他更好的解决方案,将不胜感激。