繁体   English   中英

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

[英]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 查询时得到这个。

解决方案

  1. 从模型中删除与此字段“城市”相关的属性和获取集函数; 在我们的具体案例中:
/**
 * @var \Domain\Repository\AddressRepository
 */
  1. 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');

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM