Typo3-如何在联接集合中添加自定义字段?

时间:2019-06-28 08:50:55

标签: php mysql typo3

问题

有一个扩展,我们命名为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查询时得到的。

1 个答案:

答案 0 :(得分:1)

解决方案

  1. 从模型中删除与该字段“ city”相关的属性和get-set函数;在我们的特定情况下:
/**
 * @var \Domain\Repository\AddressRepository
 */
  1. Typo3 扩展名用于具有特定的配置文件,用于自定义模型配置,例如添加新的自定义字段/Exams/Configuration/TCA/domain_model_exam.php此文件必须以 return 数组的形式添加一个新元素,其中包含此新字段的特定配置:
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');

对于这些特定要求的任何其他更好的解决方案,将不胜感激。