代码之家  ›  专栏  ›  技术社区  ›  Major Productions

symfony 3.4-PropertyAccessor异常,ChoiceType除外

  •  0
  • Major Productions  · 技术社区  · 7 年前

    这真奇怪。我得到了以下异常:

    PropertyAccessor需要操作对象或数组的图形, 但它在尝试遍历路径“quantity”时发现类型“double” 属性“数量”。

    使用以下代码:

    $choices = array();
    
    $minimumSaleUnit = $cartItem->getProduct()->getMinimumSaleUnit();
    $maxItems = $cartItem->getProduct()->getQuantity();
    
    for ($i = 1; $i <= 20; $i++) {
        $value = $i * $minimumSaleUnit;
    
        if ($value <= $maxItems) {
            $choices["$value"] = $value;
        }
    }
    
    $form = $this->createFormBuilder($cartItem)
        ->add('quantity', ChoiceType::class, ['choices' => $choices, 'choice_label' => 'Quantity'])
        ->getForm();
    

    $choices 值数组:

    enter image description here

    所以我不确定为什么会发生异常。我可不像是直接给它喂食。有什么想法吗?

    0 回复  |  直到 7 年前
        1
  •  2
  •   ejuhjav    7 年前

    这是错误指向的“choice_label”定义,即错误所说的choices数组中没有“quantity”属性路径(请参见 doctrine choice_label )其目的可能是 'label' => 'Quantity' ?