【问题标题】:doctrine2 zf2 form collection not render element学说2 zf2表单集合不呈现元素
【发布时间】:2014-06-02 19:28:43
【问题描述】:

更新:如果我升级到 2.2.6,我只工作到 ZF2 的 2.2.5 版本,该项目不会显示在表单上。

  • ZF2 2.3.1
  • PHP 5.4.4
  • 教义2:大师

我对表单的 bind() 有问题。当我尝试显示 Zend\Form\Collection 字段集的元素时,显示为空。

我已经查看了教程Doctrine Hydrator,但我无法修复它。

实体之间的关系很简单:

  • 产品 (OneToMany) $images
  • 图片(多对一)$product

这仅在我添加新产品时发生,如果我编辑带有图像的产品(操作编辑),FormCollection 元素显示在表单上。

产品实体

class Product 
{

    /**
     * @var int
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     *
     */
    protected $id;

    /**
     * @var string
     * @ORM\Column(type="string", length=255, unique=false, nullable=true)
     */
    protected $name;

    /**
     * @ORM\OneToMany(targetEntity="Image", mappedBy="product", cascade={"persist"})
     */
    protected $images;

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

    public function addImages(Collection $images)
    {
        foreach ($images as $image)
        {
            $image->setProduct($this);
            $this->images->add($image);
        }
    }

    public function removeImages(Collection $images)
    {
        foreach ($images as $image)
        {
            $image->setProduct(null);
            $this->images->removeElement($image);
        }
    }

    public function getImages()
    {
        return $this->images;
    }

}

图像实体

class Image
{
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="Product", inversedBy="images")
     * @ORM\JoinColumn(name="product_id", referencedColumnName="id", onDelete="CASCADE")
     */
    protected $product;

    public function setProduct(Product $product = null)
    {
        $this->product = $product;
    }

    public function getProduct()
    {
        return $this->product;
    }
}

产品形式

class ProductForm extends Form implements InputFilterProviderInterface
{

    public function __construct($sl)
    {
        $objectManager = $sl->get('Doctrine\ORM\EntityManager');

        parent::__construct('product-form');

        $this->setAttribute('enctype', 'multipart/form-data')
             ->setAttribute('method', 'post')
             ->setHydrator(new DoctrineHydrator($objectManager));

        // Add the user fieldset, and set it as the base fieldset
        $productFieldset = new ProductFieldset($sl);
        $productFieldset->setName('product');
        $productFieldset->setUseAsBaseFieldset(true);
        $this->add($productFieldset);

        $this->add(array(
            'name' => 'submit',
            'attributes' => array(
                'type' => 'submit',
                'value' => 'Go',
                'id' => 'submitbutton',
                'class' => 'btn btn-primary'
            )
        ));


        $this->setValidationGroup(array(
            'product',
        ));
    }

产品字段集

class ProductFieldset extends Fieldset implements InputFilterProviderInterface
{

    public function __construct($sl)
    {
        $objectManager = $sl->get('Doctrine\ORM\EntityManager');

        parent::__construct('product');

        $this->setHydrator(new DoctrineHydrator($objectManager))
             ->setObject(new Product());


        $this->add(array(
            'name' => 'id',
            'type' => 'Zend\Form\Element\Hidden'
        ));

        $this->add(array(
            'type' => 'Zend\Form\Element\Collection',
            'name' => 'images',
            'options' => array(
                'count' => 1,
                'target_element' => new ImageFieldset($objectManager)
            )
        ));
   }
}

图像字段集

class ImageFieldset extends Fieldset implements InputFilterProviderInterface
{

        public function __construct($objectManager)
        {
            parent::__construct('image');


            $this->setHydrator(new DoctrineHydrator($objectManager))
                 ->setObject(new Image());

            $this->add(array(
                        'name' => 'id',
                        'type' => 'Zend\Form\Element\Hidden'
                        )
            );

            $this->add(array(
                        'name' => 'filename',
                        'type' => 'Zend\Form\Element\File',
                        'options' => array(
                            'label' => 'Photo Upload',
                            'label_attributes' => array(
                                'class' => 'form-label'
                            ),
                            'multiple' => true,
                            'id' => 'filename'
                        )
                    )
            );
        }

        public function getInputFilterSpecification()
            {
                return array(
                   'id' => array(
                        'required' => false
                    ),
                   'filename' => array(
                        'required'    => true,
                    )
                );
        }
    }

控制器

public function addAction()
{

        $sl = $this->getServiceLocator();

        $form = new ProductForm($sl);

        $product = new Product();

        $form->bind($product);

        if ($request->isPost()):
              ....
        endif;

        return array('form' => $form);

}

【问题讨论】:

  • 不确定是否是复制粘贴问题,但是您的产品实体缺少 protected $images; 属性。
  • 对了,我忘了贴protected $images;

标签: forms collections doctrine-orm zend-framework2 one-to-many


【解决方案1】:

也许本教程可以提供帮助 - 它是最新的: Inject DoctrineORM EntityManager in ZF2 Form

它使用formElementManager和objectManager感知。

在 init() 方法中添加元素很重要,而不是在构造函数中。

还请查看 Michael 声明的 cmets:

我刚刚发现在最新版本的 ZF2(我这里有 2.2.5)中,不再需要通过模块配置传递 entityManager。 您只需在表单类中实现“ObjectManagerAwareInterface”(以及所需的 getter + setter),您就可以通过 $this->getObjetManager() 访问。

【讨论】:

    【解决方案2】:

    也许你正面临这个问题: BC break in forms between 2.3.0 and 2.3.1

    Zend\Form 在 2.3.0 和 2.3.1 之间存在 BC 中断,我们刚刚升级并偶然发现了它。 当我们使用自定义表单元素时,我们通过 FormElementManager 获取表单实例,该站点一直在使用

    使用 FormElementManager 时的一些常规更改: Creating custom elements

    To use custom elements with the FormElementManager needs a bit more work and most likely a change in how you write and use your forms.
    

    【讨论】:

      猜你喜欢
      • 2013-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-30
      • 1970-01-01
      • 2013-08-14
      • 1970-01-01
      相关资源
      最近更新 更多