dongli5785 2013-11-10 07:57
浏览 4

在带注释的表单中显示相关项目

I have got a question regarding populating my form with entities form a many to many relation.

First my code:

Product Entity:

<?php
namespace My\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Zend\Form\Annotation;
use My\Entity\Brand;

/**
 * @ORM\Entity(repositoryClass="My\EntityRepository\Product")
 * @ORM\Table(name="product")
 * @Annotation\Hydrator("Zend\Stdlib\Hydrator\ArraySerializable")
 * @Annotation\Name("Product")
 */
class Product{

    /**
     * @ORM\id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     * @Annotation\Attributes({"type":"hidden"})
     */
    protected $id;

    /**
     * @ORM\ManyToMany(targetEntity="productGroup")
     * @Annotation\Type("Zend\Form\Element\Select")
     * @Annotation\Options({"label":"Productgroup: "})
     */
    protected $productGroups;


    /**
     * @ORM\Column(nullable=false)
     * @Annotation\Attributes({"type":"text"})
     * @Annotation\Options({"label":"Product name:"})
     */
    protected $productName;

    /**
     * @Annotation\Attributes({"type":"textarea"})
     * @Annotation\Options({"label":"Product description: "})
     * @ORM\Column
     */
    protected $description;

    public function __construct() {
        $this->memos = new ArrayCollection();
        $this->productGroups = new ArrayCollection();
    }

    /**
     * Sets the product tags
     * @param ArrayCollection $tags
     */
    public function setTags(\Doctrine\Common\Collections\ArrayCollection $tags) {
        $this->productGroups = $tags;
    }


    /**
     * This function unsets a product group
     */
    public function unsetProductsGroups() {
        unset($this->productGroups);
    }
}

Then I have My Action

 public function newAction()
    {
        $em = $this->getEntityManager();

        $request = $this->getRequest();
        $product = new Product();
        $builder = new AnnotationBuilder($em);
        $form = $builder->createForm($product);


        if ($request->isPost() && $this->request->getPost()) {
            $repo = $this->getEntityManager()->getRepository('My\Entity\Product');
            $repo->addProduct($this->getRequest()->getPost());
            $this->flashMessenger()->addMessage('The product was added.');
            return $this->redirect()->toRoute('zfcadmin');
        } else {
            $config = $this->getModuleConfig();
            if (isset($config['my_form_extra'])) {
                foreach ($config['my_form_extra'] as $field) {
                    $form->add($field);
                }
            }

            $form->setHydrator(new DoctrineHydrator($em, 'My\Entity\Product'));
            $form->bind($product);
            return new ViewModel(array('form' => $form));
        }
    }

And my View

<div class="well">
    <?php
        $form = $this->form;
        $form->setAttribute('method','post');
        echo $this->form()->openTag($form);
        echo $this->formSelect($form->get('productGroups'));
        echo $this->form()->closeTag($form);
    ?>
</div>

The output of this an empty select box. Even in my edit form where the setup is the same but the dropdown is empty and there are surely related items.

Note: I have only shown the information related to this question.

I would like to know how I can solve this problem whith showing related items. Prefferably only using annotations.

Thanks.

  • 写回答

1条回答 默认 最新

  • duanchi0883649 2013-11-10 11:43
    关注

    There is only one $product instance shown, it is a new Product with empty memo and productGroup array collections. The form has this un-managed product instance bound to it, your view is getting the form with the un-managed product instance bound, so there are no select items displayed.

    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)