[英]Typo3 - how to add a custom field in join collection?
有一個擴展名為 ProjectExams,它顯示考試列表。 有一個包含 Controller、Model、ModelRepository、Tables 和 Template 的結構。 一切都使用 Extbase 來檢索數據集合(延遲加載模型綁定)。
我們有兩個要求:
*繞過綁定並僅使用一個“原始”SQL查詢來檢索集合,並在數據庫中加入;
* 在視圖(模板)中返回一個沒有對象元素的數組
模型集合代表考試列表。 除了 UID、標題、描述和 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
}
Template(View)文件需要一個變量exams.city
在模型文件中,我們有一個帶有 set 和 get 函數的受保護 var $city
模型庫代碼
/**
* 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)
not NULL
,因為我們在對數據庫運行 SQL 查詢時得到這個。
/**
* @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');
對於這些特定請求的任何其他更好的解決方案,將不勝感激。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.