实体字段类型symfony 2.0-在编辑操作中显示数据库中的数据

实体字段类型symfony 2.0-在编辑操作中显示数据库中的数据,symfony,doctrine-orm,symfony-forms,Symfony,Doctrine Orm,Symfony Forms,情况如下: 我有3张桌子(一对多): 产品,产品标签,标签标签(标签保留-u-) 正如您可能已经猜到的,一个产品可以有许多标签 现在我有了一个显示可用标签的表单产品。对于“新建”和“创建”操作,我可以显示和保存选定的标签。 当我试图在数据库的编辑操作中显示标签并显示选定标签时,我的问题就出现了 一些代码: 实体产品: /** * @var Doctrine\Common\Collections\ArrayCollection $productLabels * * @ORM\OneToMan

情况如下:

我有3张桌子(一对多): 产品,产品标签,标签标签(标签保留-u-)

正如您可能已经猜到的,一个产品可以有许多标签

现在我有了一个显示可用标签的表单产品。对于“新建”和“创建”操作,我可以显示和保存选定的标签。 当我试图在数据库的编辑操作中显示标签并显示选定标签时,我的问题就出现了

一些代码:

实体产品:

/**
 * @var Doctrine\Common\Collections\ArrayCollection $productLabels
 *
 * @ORM\OneToMany(targetEntity="Labeyrie\Bundle\MainsiteBundle\Entity\ProductLabel", mappedBy="product")
 */
protected $productLabels;
/**
 * @var Doctrine\Common\Collections\ArrayCollection $productLabels
 *
 * @ORM\OneToMany(targetEntity="Labeyrie\Bundle\MainsiteBundle\Entity\ProductLabel", mappedBy="labelTag")
 */
protected $productLabels;
/**
 * @var Labeyrie\Bundle\MainsiteBundle\Entity\Product $product
 * 
 * @ORM\Id
 * @ORM\ManyToOne(targetEntity="Labeyrie\Bundle\MainsiteBundle\Entity\Product", inversedBy="productLabels")
 */
protected $product;

/**
 * @var Labeyrie\Bundle\MainsiteBundle\Entity\LabelTag $labelTag
 * 
 * @ORM\Id
 * @ORM\ManyToOne(targetEntity="Labeyrie\Bundle\MainsiteBundle\Entity\LabelTag", inversedBy="productLabels")
 */
protected $labelTag;
->add('productLabels', 'entity', array(
            'class' => 'LabeyrieMainsiteBundle:LabelTag',
            'property' => 'title',
            'multiple' => true,
            'expanded' => true,
        ))
if ($form->isValid()) {
        /* save product */
        $em->persist($product);
        $em->flush();

        /* save product labels */
        $labels = $product->getProductLabels();
        if (!empty($labels)) {
            foreach ($labels as $label) {
                $productLabel = new ProductLabel();
                $productLabel->setLabelTag($label);
                $productLabel->setProduct($product);

                $em->persist($productLabel);
            }
        }
        $em->flush();
    }
$entity = $em->getRepository('MyProjectBundle:Product')->find($id));   

    $form = $this->createForm(new ProductType(), $entity);
实体标签标签:

/**
 * @var Doctrine\Common\Collections\ArrayCollection $productLabels
 *
 * @ORM\OneToMany(targetEntity="Labeyrie\Bundle\MainsiteBundle\Entity\ProductLabel", mappedBy="product")
 */
protected $productLabels;
/**
 * @var Doctrine\Common\Collections\ArrayCollection $productLabels
 *
 * @ORM\OneToMany(targetEntity="Labeyrie\Bundle\MainsiteBundle\Entity\ProductLabel", mappedBy="labelTag")
 */
protected $productLabels;
/**
 * @var Labeyrie\Bundle\MainsiteBundle\Entity\Product $product
 * 
 * @ORM\Id
 * @ORM\ManyToOne(targetEntity="Labeyrie\Bundle\MainsiteBundle\Entity\Product", inversedBy="productLabels")
 */
protected $product;

/**
 * @var Labeyrie\Bundle\MainsiteBundle\Entity\LabelTag $labelTag
 * 
 * @ORM\Id
 * @ORM\ManyToOne(targetEntity="Labeyrie\Bundle\MainsiteBundle\Entity\LabelTag", inversedBy="productLabels")
 */
protected $labelTag;
->add('productLabels', 'entity', array(
            'class' => 'LabeyrieMainsiteBundle:LabelTag',
            'property' => 'title',
            'multiple' => true,
            'expanded' => true,
        ))
if ($form->isValid()) {
        /* save product */
        $em->persist($product);
        $em->flush();

        /* save product labels */
        $labels = $product->getProductLabels();
        if (!empty($labels)) {
            foreach ($labels as $label) {
                $productLabel = new ProductLabel();
                $productLabel->setLabelTag($label);
                $productLabel->setProduct($product);

                $em->persist($productLabel);
            }
        }
        $em->flush();
    }
$entity = $em->getRepository('MyProjectBundle:Product')->find($id));   

    $form = $this->createForm(new ProductType(), $entity);
实体产品标签:

/**
 * @var Doctrine\Common\Collections\ArrayCollection $productLabels
 *
 * @ORM\OneToMany(targetEntity="Labeyrie\Bundle\MainsiteBundle\Entity\ProductLabel", mappedBy="product")
 */
protected $productLabels;
/**
 * @var Doctrine\Common\Collections\ArrayCollection $productLabels
 *
 * @ORM\OneToMany(targetEntity="Labeyrie\Bundle\MainsiteBundle\Entity\ProductLabel", mappedBy="labelTag")
 */
protected $productLabels;
/**
 * @var Labeyrie\Bundle\MainsiteBundle\Entity\Product $product
 * 
 * @ORM\Id
 * @ORM\ManyToOne(targetEntity="Labeyrie\Bundle\MainsiteBundle\Entity\Product", inversedBy="productLabels")
 */
protected $product;

/**
 * @var Labeyrie\Bundle\MainsiteBundle\Entity\LabelTag $labelTag
 * 
 * @ORM\Id
 * @ORM\ManyToOne(targetEntity="Labeyrie\Bundle\MainsiteBundle\Entity\LabelTag", inversedBy="productLabels")
 */
protected $labelTag;
->add('productLabels', 'entity', array(
            'class' => 'LabeyrieMainsiteBundle:LabelTag',
            'property' => 'title',
            'multiple' => true,
            'expanded' => true,
        ))
if ($form->isValid()) {
        /* save product */
        $em->persist($product);
        $em->flush();

        /* save product labels */
        $labels = $product->getProductLabels();
        if (!empty($labels)) {
            foreach ($labels as $label) {
                $productLabel = new ProductLabel();
                $productLabel->setLabelTag($label);
                $productLabel->setProduct($product);

                $em->persist($productLabel);
            }
        }
        $em->flush();
    }
$entity = $em->getRepository('MyProjectBundle:Product')->find($id));   

    $form = $this->createForm(new ProductType(), $entity);
产品形式:

/**
 * @var Doctrine\Common\Collections\ArrayCollection $productLabels
 *
 * @ORM\OneToMany(targetEntity="Labeyrie\Bundle\MainsiteBundle\Entity\ProductLabel", mappedBy="product")
 */
protected $productLabels;
/**
 * @var Doctrine\Common\Collections\ArrayCollection $productLabels
 *
 * @ORM\OneToMany(targetEntity="Labeyrie\Bundle\MainsiteBundle\Entity\ProductLabel", mappedBy="labelTag")
 */
protected $productLabels;
/**
 * @var Labeyrie\Bundle\MainsiteBundle\Entity\Product $product
 * 
 * @ORM\Id
 * @ORM\ManyToOne(targetEntity="Labeyrie\Bundle\MainsiteBundle\Entity\Product", inversedBy="productLabels")
 */
protected $product;

/**
 * @var Labeyrie\Bundle\MainsiteBundle\Entity\LabelTag $labelTag
 * 
 * @ORM\Id
 * @ORM\ManyToOne(targetEntity="Labeyrie\Bundle\MainsiteBundle\Entity\LabelTag", inversedBy="productLabels")
 */
protected $labelTag;
->add('productLabels', 'entity', array(
            'class' => 'LabeyrieMainsiteBundle:LabelTag',
            'property' => 'title',
            'multiple' => true,
            'expanded' => true,
        ))
if ($form->isValid()) {
        /* save product */
        $em->persist($product);
        $em->flush();

        /* save product labels */
        $labels = $product->getProductLabels();
        if (!empty($labels)) {
            foreach ($labels as $label) {
                $productLabel = new ProductLabel();
                $productLabel->setLabelTag($label);
                $productLabel->setProduct($product);

                $em->persist($productLabel);
            }
        }
        $em->flush();
    }
$entity = $em->getRepository('MyProjectBundle:Product')->find($id));   

    $form = $this->createForm(new ProductType(), $entity);
下面是我如何将其保存在创建操作:

/**
 * @var Doctrine\Common\Collections\ArrayCollection $productLabels
 *
 * @ORM\OneToMany(targetEntity="Labeyrie\Bundle\MainsiteBundle\Entity\ProductLabel", mappedBy="product")
 */
protected $productLabels;
/**
 * @var Doctrine\Common\Collections\ArrayCollection $productLabels
 *
 * @ORM\OneToMany(targetEntity="Labeyrie\Bundle\MainsiteBundle\Entity\ProductLabel", mappedBy="labelTag")
 */
protected $productLabels;
/**
 * @var Labeyrie\Bundle\MainsiteBundle\Entity\Product $product
 * 
 * @ORM\Id
 * @ORM\ManyToOne(targetEntity="Labeyrie\Bundle\MainsiteBundle\Entity\Product", inversedBy="productLabels")
 */
protected $product;

/**
 * @var Labeyrie\Bundle\MainsiteBundle\Entity\LabelTag $labelTag
 * 
 * @ORM\Id
 * @ORM\ManyToOne(targetEntity="Labeyrie\Bundle\MainsiteBundle\Entity\LabelTag", inversedBy="productLabels")
 */
protected $labelTag;
->add('productLabels', 'entity', array(
            'class' => 'LabeyrieMainsiteBundle:LabelTag',
            'property' => 'title',
            'multiple' => true,
            'expanded' => true,
        ))
if ($form->isValid()) {
        /* save product */
        $em->persist($product);
        $em->flush();

        /* save product labels */
        $labels = $product->getProductLabels();
        if (!empty($labels)) {
            foreach ($labels as $label) {
                $productLabel = new ProductLabel();
                $productLabel->setLabelTag($label);
                $productLabel->setProduct($product);

                $em->persist($productLabel);
            }
        }
        $em->flush();
    }
$entity = $em->getRepository('MyProjectBundle:Product')->find($id));   

    $form = $this->createForm(new ProductType(), $entity);
没问题

问题出现在我的编辑操作中:

/**
 * @var Doctrine\Common\Collections\ArrayCollection $productLabels
 *
 * @ORM\OneToMany(targetEntity="Labeyrie\Bundle\MainsiteBundle\Entity\ProductLabel", mappedBy="product")
 */
protected $productLabels;
/**
 * @var Doctrine\Common\Collections\ArrayCollection $productLabels
 *
 * @ORM\OneToMany(targetEntity="Labeyrie\Bundle\MainsiteBundle\Entity\ProductLabel", mappedBy="labelTag")
 */
protected $productLabels;
/**
 * @var Labeyrie\Bundle\MainsiteBundle\Entity\Product $product
 * 
 * @ORM\Id
 * @ORM\ManyToOne(targetEntity="Labeyrie\Bundle\MainsiteBundle\Entity\Product", inversedBy="productLabels")
 */
protected $product;

/**
 * @var Labeyrie\Bundle\MainsiteBundle\Entity\LabelTag $labelTag
 * 
 * @ORM\Id
 * @ORM\ManyToOne(targetEntity="Labeyrie\Bundle\MainsiteBundle\Entity\LabelTag", inversedBy="productLabels")
 */
protected $labelTag;
->add('productLabels', 'entity', array(
            'class' => 'LabeyrieMainsiteBundle:LabelTag',
            'property' => 'title',
            'multiple' => true,
            'expanded' => true,
        ))
if ($form->isValid()) {
        /* save product */
        $em->persist($product);
        $em->flush();

        /* save product labels */
        $labels = $product->getProductLabels();
        if (!empty($labels)) {
            foreach ($labels as $label) {
                $productLabel = new ProductLabel();
                $productLabel->setLabelTag($label);
                $productLabel->setProduct($product);

                $em->persist($productLabel);
            }
        }
        $em->flush();
    }
$entity = $em->getRepository('MyProjectBundle:Product')->find($id));   

    $form = $this->createForm(new ProductType(), $entity);

如果我只渲染表单,我只会看到标签,那些被选中的信息不会被发送(因为它在我管理的中间表中),因此没有显示。


我需要使用我的一对多架构来解决这个问题。表单配置是否错误?请帮忙。:)

我终于找到了答案:

只需执行以下操作:

    $collection = new ArrayCollection();
    $labels = $entity->getProductLabels();
    if (!empty($labels)) {
        foreach ($labels as $label) {
            $collection->add($label->getLabelTag());
        }
    }

    $form->get('productLabels')->setData($collection); 
检索单个对象(LabelTag),将其放入数组集合中,将集合传递给表单字段的数据,就完成了

将显示该表单并显示所选选项