【发布时间】: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