对于某些,但不是全部,表单提交得到了"无法确定属性的访问类型" id"。"

时间:2017-06-18 10:36:04

标签: php symfony doctrine symfony-forms symfony-3.3

我有一个复杂的表单,映射到Entity的集合,允许以特定的事件价格购买机票,对于它工作的大部分事件,但对于其中一个,我们进入一个可重复的方式错误Could not determine access type for property "id"

在这种情况下,我知道Could not determine access type for property X是因为缺少了一个setter。确实没有setId()方法而idprotected,但我认为symfony不应该首先尝试设置id(因为它适用于其他形式,即购买门票,并正确显示链接到事件等。)

所以我的问题是为什么在某些情况下symfony需要setId()

我有以下实体

class OrderFront
{
    use Traits\HasId;
    /**
     * List of pricings set on this event
     *
     * @Assert\Valid()
     * @ORM\OneToMany(  
     *     targetEntity="QuantityByPricing",
     *     mappedBy="orderFront",
     *     cascade={"persist", "remove"}
     * )
     */
    private $quantitiesByPricing;

    /**
     * @Assert\NotBlank()
     * @Assert\NotNull()
     */
    public $occurenceId;

    public function getQuantitiesByPricing(): Collection
    {
        return $this->quantitiesByPricing;
    }

    public function addQuantitiesByPricing(QuantityByPricing $quantityByPricing)
    {
        $quantityByPricing->setOrderFront($this);
        $this->quantitiesByPricing[] = $quantityByPricing;
        return $this;
    }

}

class QuantityByPricing
{
    use Traits\HasId;

    /**
     * @var int
     * @ORM\Column(type="integer")
     */
    public $quantity = 0;

    /**
     * The pricing of this ticket
     *
     * @var Pricing
     *
     * @ORM\ManyToOne(targetEntity="Pricing")
     * @ORM\JoinColumn(
     *     name="pricing_id",
     *     nullable=false,
     *     referencedColumnName="id"
     * )
     */
    public $pricing;
}

确实是特质" HasId"没有制定者(但它是故意的)或至少到现在为止从未出现问题

trait HasId
{
    /** 
     *
     * @ORM\GeneratedValue(strategy="UUID")
     * @ORM\Column(name="id", type="guid") }
     * @ORM\Id
     *
     * @Assert\Uuid()
     */
    private $id;

    /**
     * Get id
     *
     * @return guid
     */
    public function getId()
    {
        return $this->id;
    }
}

表格

    class OrderType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {   
            $builder
                ->add(
                    'quantitiesByPricing',
                    CollectionType::class,
                    ['entry_type' => QuantityByPricingType::class]
                )
                ->add('occurenceId', HiddenType::class)
           ;
        }
        public function configureOptions(OptionsResolver $resolver)
        {   
            $resolver->setDefaults(['data_class' => 'AppBundle\Entity\OrderFront']);
        }
    }

    /**
     * sub-form to buy X tickets of a given pricing
     */
    class QuantityByPricingType extends AbstractType
    {   
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('quantity')
                ->add('pricing',HiddenPricingType::class)
            ;
        }

        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults(['data_class' => 'AppBundle\Entity\QuantityByPricing']);
        }
    }

    /**
     *
     */
    class HiddenPricingType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {   
            $builder->add('id', HiddenType::class);
        }

        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults(['data_class' => 'AppBundle\Entity\Pricing']);
        }
    }

class Pricing
{
    use Traits\HasId;
}

创建表单的控制器如下所示

      // call the DB to set the possible pricings
      // inside it calls addQuantityByPricing
      $orderFront = $this->_createOrderFront(); 
      $form = $this->createForm(OrderType::class, $orderFront);
      $form->handleRequest($request);

异常回溯是以下

Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException:
Could not determine access type for property "id".

  at vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php:652
  at Symfony\Component\PropertyAccess\PropertyAccessor->writeProperty(array(object(Pricing), object(Pricing)), 'id', '80424145-ca68-4dce-b4f0-644a423d3aad')

添加一些debug时,我可以看到2 Pricing

array:2 [▼
  0 => Pricing {#1375 ▶}   # id  "d82cafb8-432b-4e20-ac9f-66e48dc55458"
  1 => & Pricing {#1375 ▶} # id  "d82cafb8-432b-4e20-ac9f-66e48dc55458"
]

所以似乎symfony试图通过其他有效/现有的定价来覆盖此定价的ID(有效/现有定价),这就是我认为它试图"替换&#的原因34;它,通过尝试调用setter并失败,但为什么会这样做?

编辑:

经过一些调试后,我发现了一个令人不安的巧合:

  • d82cafb8-432b-4e20-ac9f-66e48dc55458是我在addQuantitiesByPricing方法中添加一些调试时添加的第一个ID
  • 80424145-ca68-4dce-b4f0-644a423d3aad是提交表单中索引为0的定价ID

Edit2:我的控制器创建表单的方式(以及调用addQuantitiesByPricing使d82cafb8-432b-4e20-ac9f-66e48dc55458首先出现的那个),使我们在获取Ids之前首先从数据库中检索这些ID POST方法

1 个答案:

答案 0 :(得分:0)

问题在于,在创建表单的主要实体(即addQuantitiesByPricing)时使用OrderFront添加元素的顺序与在HTML中添加元素的顺序不同(在树枝文件中隐藏了sort

因此,QuantityByPricing的POSTed数组与数据库创建的QuantityByPricing数组的顺序不同,所以当Symfony正在发挥作用以查看需要更新哪个字段时,它看到我的数组的第一个元素与POSTed数组的第一个元素有不同的id,然后尝试通过首先搜索它的setter来替换它,所以setId()因此异常

之前我没有看到这个错误的原因是因为一些(非)幸运的原因来自数据库的数组已经排序,所以树枝排序无效

注意:不直接与此特定上下文相关,但如果发布数组的大小小于数据库数组的大小,则将获得相同的堆栈跟踪(除了最后一个参数将为null)< / p>

Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException:
Could not determine access type for property "id".

  at vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php:652
  at Symfony\Component\PropertyAccess\PropertyAccessor->writeProperty(array(object(Pricing), object(Pricing)), 'id', null)

注意2:另一个可能有效的解决方案是在使用PRE_SUBMIT

中的OrderType事件提交数组之前重新排序数组
        ->addEventListener(
            FormEvents::PRE_SUBMIT,
            function (FormEvent $event) {
                $submittedData = $event->getData();
                if (!array_key_exists('quantitiesByPricing', $submittedData)) {
                    return;
                }
                $form = $event->getForm();
                dump($submittedData['quantitiesByPricing']);

                //Re-index the array to ensure the forms
                // stay in the submitted order.
                $submittedData['quantitiesByPricing'] = /* some code to reorder the submitted form in the same order as the data from the database */

                $event->setData($submittedData);
            }
        )