【问题标题】:Extbase Property Mapping for arrays数组的 Extbase 属性映射
【发布时间】:2017-05-02 18:29:17
【问题描述】:

我想使用 Property Mapper 在 Fluid 表单中使用数组对象。如果用户点击“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". 

隐藏字段解析为:&lt;input name="tx_myshop_pi1[newOrder][orderProduct][0][product]" value="8" type="hidden"&gt;(静态值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
        );
    }
}

【问题讨论】:

  • tx_myshop_pi1[newOrder][orderProduct][0] 来自 Product 类是否正确?因为这就是异常所说的。我假设您的模型产品不包含名为产品的属性。
  • tx_myshop_pi1[newOrder][orderProduct][0] 来自 ObjectStorage 类,tx_myshop_pi1[newOrder][orderProduct][0][product] 来自 Product 类。我为模型代码添加了 pastebin 链接 - 您可以检查这是否是错误源。如果有必要进行某种类型转换,我可以使用一些帮助来了解如何做到这一点。
  • 已解决 - 错误在模型中,而不是在 PropertyMapping 中
  • tx_myshop_pi1[newOrder][orderProduct] 来自 ObjectStorage 类。 [0] 已经是存储中的第一项。但是,是的,你已经解决了!

标签: typo3 extbase typo3-7.6.x


【解决方案1】:

我是这样解决的,在我的例子中,一个问题的答案是动态的,有两个字段,每个字段:

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

【讨论】:

  • 'allowCreationForSubProperty' 在 7.6 中不可用 - 但我认为我使用的 setTypeConverterOption 也是如此?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-05
  • 2017-11-27
相关资源
最近更新 更多