显示带有“foreign_table”选项填充的流体的选择字段

时间:2016-06-30 14:49:50

标签: typo3 fluid typo3-7.6.x

我使用前端插件创建了一个typo3扩展,您可以在其中创建新条目。

一个字段是一个包含来自另一个表的值的选择字段。在TCA中它看起来像这样:

'shoeref' => array(
    'exclude' => 0,
    'label' => 'Shoe Ref Test',
    'config' => array(
        'type' => 'select',
        'foreign_table' => 'tx_testcollection_domain_model_testcollection',
        'foreign_table_where' => 'and tx_testcollection_domain_model_testcollection.pid = 96 and tx_testcollection_domain_model_testcollection.sys_language_uid = 0',
        'minitems' => 0,
        'maxitems' => 1,
    ),
),

这适用于后端。我可以从表tx_testcollection_domain_model_testcollection中选择一条记录,也可以在前端我可以访问这个元素。

但是在新的' Page(您可以在其中创建新元素)我不知道如何填充选择字段。

我刚试过:

<f:form.select property="shoeref" />

但它说我选择&#39;是必要的。
像这样:

<f:form.select property="shoeref" options="{0: 'test1', 1: 'test2'}"/>

但是我当然需要这个选择域中表tx_testcollection_domain_model_testcollection的值。

那么如何在该选择字段中获取该表的值(具有相同的where-条件)?

编辑我很接近:
我在newAction中的控制器中填充了一个变量:

    $collection = $this->objectManager->get('Test\TestCollection\Domain\Repository\TestCollectionRepository');

    $querySettings = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings');
    $querySettings->setRespectStoragePage(FALSE);
    $querySettings->setStoragePageIds(array(96)); //Looks like he dont care about this and just finds all..

    $collection->setDefaultQuerySettings($querySettings);
    $this->view->assign('collecton',$collection->findAll());

在模板中:

<f:form.select property="shoeref" options="{collecton}" optionLabelField="name"/>

我看到了正确的选项,但是当我想保存时,我得到:

    Exception while property mapping at property path "": PHP Catchable Fatal Error: Argument 1 passed to Test\TestCustomerfeedback\Domain\Model\Customerfeedback::setShoeref() must be an instance of
 Test\TestCustomerfeedback\Domain\Model\Test\TestCollection\Domain\Model\TestCollection
, instance of 
Test\TestCollection\Domain\Model\TestCollection
 given

, called in /home/www-data/typo3_src-7.6.4_dev/typo3/sysext/extbase/Classes/Reflection/ObjectAccess.php on line 209 and defined in /home/www-data/mydomain/typo3conf/ext/test_customerfeedback/Classes/Domain/Model/Customerfeedback.php line 32

1 个答案:

答案 0 :(得分:1)

在methoid Test\TestCustomerfeedback\Domain\Model\Customerfeedback::setShoeref()中参数的类型提示是错误的 - 它可能看起来像这样:

public function setShoeref(Test\TestCollection\Domain\Model\TestCollection $shoeRef)
{
    // ...
}

应该看起来像这样:

public function setShoeref(\Test\TestCollection\Domain\Model\TestCollection $shoeRef)
{
    // ...
}

(参数类型中的附加反斜杠)

use语句添加到您的类文件的头部会更好,或者,在这种特殊情况下(因为类CustomerFeedbackTestCollection都在同一个名称空间),只需使用短TestCollection作为类型提示:

<?php

public function setShoeref(TestCollection $shoeRef)
{
    // ...
}

另一件事:不要使用对象管理器获取您的存储库,将它们注入使用它们的类中 - 您可以通过使应该包含它们的属性受到保护,将所需的类添加为{{1}注释,并添加注释@var。然后在再次运行该代码之前清除安装工具中的缓存。

@inject
相关问题