数组的Extbase属性映射

时间:2017-05-02 18:29:18

标签: typo3 extbase typo3-7.6.x

我想使用Property Mapper在Fluid表单中使用Array对象。如果用户点击“add_product”链接,则会动态添加产品:

<f:form action="property" name="newOrder" object="{newOrder}">

    <f:for each="{newOrder.orderProduct}" as="orderProduct" iteration="iterator">
        <f:form.hidden property="orderProduct.{iterator.index}.product" value="8" />
        <h3>OrderProduct: {orderProduct.product.title}</h3>
    </f:for>
    <f:form.hidden name="add_product" value="1" />

    <input type="submit" value="submit" />
</f:form>

提交后我得到的是这个例外。

Uncaught TYPO3 Exception #1297759968: 
Exception while property mapping at property path "orderProduct.0":
Property "product" was not found in target object of type "MyVendor\MyShop\Domain\Model\Product". 

隐藏字段解析为:<input name="tx_myshop_pi1[newOrder][orderProduct][0][product]" value="8" type="hidden">(静态值8只是为了简化示例)

我还尝试key="key"而不是迭代器,空括号orderProduct[],使用名称而不是没有结果的属性。

这是(简化的)调试输出:

newOrder (MyVendor\MyShop\Domain\Model\ShopOrder)
   => orderProduct (TYPO3\CMS\Extbase\Persistence\ObjectStorage)
      3222112 => MyVendor\MyShop\Domain\Model\OrderProduct
              product => MyVendor\MyShop\Domain\Model\Product
                     uid => 8
                     title => 'Product1'  

这是型号代码:

对于Property Mapper,我尝试了很多配置而没有成功。在我看来,这应该有效,但事实并非如此:

public function initializePropertyAction()
{
    /** @var \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration $propertyMappingConfiguration */
    $propertyMappingConfiguration = $this->arguments['newOrder']->getPropertyMappingConfiguration();
    $propertyMappingConfiguration->allowAllProperties();
    $propertyMappingConfiguration->setTypeConverterOption('TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter',
        PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED,
        TRUE);

    $propertyMappingConfiguration->forProperty('orderProduct')->allowAllProperties();
    $propertyMappingConfiguration->forProperty('orderProduct')->setTypeConverterOption(
        'TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter',
        PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED,
        TRUE
    );

    //workaround from https://forge.typo3.org/issues/61628
    for ($i = 0; $i < 99; $i++) {
        $propertyMappingConfiguration->forProperty('orderProduct.' . $i)->allowAllProperties();
        $propertyMappingConfiguration->forProperty('orderProduct.' . $i . '.*')->allowAllProperties();
        $propertyMappingConfiguration->forProperty('orderProduct.' . $i)->setTypeConverterOption(
            'TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter',
            PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED,
            TRUE
        );
    }
}

1 个答案:

答案 0 :(得分:0)

我解决了这个问题,就我的情况来说,这个问题的动态答案有两个字段,每个字段都有:

$propertyMappingConfiguration = $this->arguments->getArgument('question')->getPropertyMappingConfiguration();
$propertyMappingConfiguration->skipProperties('category');
$propertyMappingConfiguration->allowProperties('answers');         
$propertyMappingConfiguration->forProperty('answers.*')->allowProperties('answerField1', 'answerField2');
$propertyMappingConfiguration->allowCreationForSubProperty('answers.*');
$propertyMappingConfiguration->allowModificationForSubProperty('answers.*');