使用query_builder和多个true将数据设置为实体字段

时间:2016-10-07 11:51:19

标签: php symfony symfony-forms

我有以下字段

        ->add('specialProduct' , 'entity', array(
            'class' => 'BSSmartDistributionBundle:Product',
            'expanded' => true,
            'multiple' => true,
            'required' => true,
            'label' => false,
            'mapped' => false,
            'query_builder' => function(ProductRepository $er) {
                return $er->createQueryBuilder('p')
                    ->where('p.active = :active')
                    ->andWhere('p.type = :type')
                    ->setParameter('type', 1)
                    ->setParameter('active', 1);
            },
            'data' => $this->specialProduct # not working
        ))

我获取数据并将其存储在会话

$specialProduct = $form->get('specialProduct')->getData();
$request->getSession()->set('specialProduct', $specialProduct);

数据:

Doctrine\Common\Collections\ArrayCollection Object
(
[elements:Doctrine\Common\Collections\ArrayCollection:private] =>   Array
    (
        [0] => BS\SmartDistributionBundle\Entity\Product Object
            (
                [id:BS\SmartDistributionBundle\Entity\Product:private] => 6
                [type:BS\SmartDistributionBundle\Entity\Product:private] => 1
                [name:BS\SmartDistributionBundle\Entity\Product:private] => Starter Pack
                [duration:BS\SmartDistributionBundle\Entity\Product:private] => 1
                [description:BS\SmartDistributionBundle\Entity\Product:private] => Tout de suite
                [full_description:BS\SmartDistributionBundle\Entity\Product:private] => blabla
                [price:BS\SmartDistributionBundle\Entity\Product:private] => 39
                [picture:BS\SmartDistributionBundle\Entity\Product:private] => /tmp/php6h3rXK
                [active:BS\SmartDistributionBundle\Entity\Product:private] => 1
            )

        [1] => BS\SmartDistributionBundle\Entity\Product Object
            (
                [id:BS\SmartDistributionBundle\Entity\Product:private] => 15
                [type:BS\SmartDistributionBundle\Entity\Product:private] => 1
                [name:BS\SmartDistributionBundle\Entity\Product:private] => asdsd
                [duration:BS\SmartDistributionBundle\Entity\Product:private] => 2
                [description:BS\SmartDistributionBundle\Entity\Product:private] => asdasd
                [full_description:BS\SmartDistributionBundle\Entity\Product:private] => adsd
                [price:BS\SmartDistributionBundle\Entity\Product:private] => 12
                [picture:BS\SmartDistributionBundle\Entity\Product:private] => uploads/product/d978b90215d06fbf569203e7cf2d5aef.png
                [active:BS\SmartDistributionBundle\Entity\Product:private] => 1
            )

    )

)

现在我想查看相应的复选框!?

具有多个false的相同字段我可以使用

设置值
'data' => $this->value

我试过

'data' => $this->specialProduct # not working (contains $specialProduct above)

如何设置这些值? 我需要传递数组吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

表单的默认值始终在FormInterface的{​​{1}}实例上使用setData,而不是在每个表单字段的options数组中dataThe docs says

  

表单字段的默认值直接取自底层数据结构(例如实体或数组)。数据选项会覆盖此默认值。

如果您使用data选项,则表单始终会预先选择data中的内容,而不是来自实体。

因此,如果我正确理解您的代码,那就是:

$form->setData([
    specialProduct => $request->getSession()->get('specialProduct')
]);

因为会话中存储的数据是Product s。

的集合

答案 1 :(得分:0)

我结束了这样做:

我正在存储并向表单传递一个arrayCollection

controller :
$specialProduct = $form->get('specialProduct')->getData();
$request->getSession()->set('specialProduct', $specialProduct);

form :
$this->specialProduct  = $options['specialProduct'];

'data' => $this->specialProduct

而是我使用只有id的数组

foreach ($specialProduct as $product) {
    $specialProductIds[] = $product->getId();
}

$request->getSession()->set('specialProductIds', $specialProductIds);

将其传递给具有实体管理器的表单

$this->specialProductIds  = $options['specialProductIds'];
$this->em                 = $options['em'];

形式

public function availablespcialProduct($ids) {

    if ($this->em) {
        return $this->em
        ->getRepository('BS\SmartDistributionBundle\Entity\Product')
        ->findById(
            $ids
        );
    }
}

最后

'data'     => $this->availablespcialProduct($this->specialProductIds)

仍在等待更清洁的解决方案!

谢谢!