Doctrine orm Zf2列出实体原则2中的元素
我有一个简单的问题, 如何创建表单列表元素,如网格或以下内容:Doctrine orm Zf2列出实体原则2中的元素,doctrine-orm,zend-framework2,zend-form2,Doctrine Orm,Zend Framework2,Zend Form2,我有一个简单的问题, 如何创建表单列表元素,如网格或以下内容: [x] name | image | [button] [ ] name | image | [button] [x] name | image | [button] <table> <tr><th>checkbox</th><th>name</th><th>action</th></tr> <tr><td
[x] name | image | [button]
[ ] name | image | [button]
[x] name | image | [button]
<table>
<tr><th>checkbox</th><th>name</th><th>action</th></tr>
<tr><td><input type="checkbox"></td><td>name</td><td><button>OK</td></tr>
<tr><td><input type="checkbox"></td><td>name</td><td><button>OK</td></tr>
<tr><td><input type="checkbox"></td><td>name</td><td><button>OK</td></tr>
</table>
我在Zend\form\Element\Collection中使用了表单,但我不知道如何从db填充集合日期,所以我有了清晰的表单
我应该正确地使用它,以及使用什么?从Doctrine中,您已经获得了一个iterable数据类型(数组)。因此,您只需在视图中对其进行迭代:
...
<?php foreach($this->data as $area): ?>
//your table row markup for a single entity
<?php endforeach; ?>
...
。。。
//单个实体的表行标记
...
免责声明:,没有回答。因此,我也很想知道“Zend”的方式,或者是否有人能够提出替代方案
下面的方法似乎适合我
ListForm.php
将集合添加到“列表”表单中
/** The collection that holds each element **/
$name = $this->getCollectionName();
$collectionElement = new \Zend\Form\Element\Collection($name);
$collectionElement->setOptions(array(
'count' => 0,
'should_create_template' => false,
'allow_add' => true
));
$this->add($collectionElement);
此集合将保留集合元素(Zend\Form\element\Checkbox
)
然后我添加了一些方法,允许我将arraycollection
传递给表单。对于集合中的每个实体,我将创建一个新的$targetElement
;将其选中值设置为实体的id
/**
* addItems
*
* Add multiple items to the collection
*
* @param Doctrine\Common\Collections\Collection $items Items to add to the
* collection
*/
public function addItems(Collection $items)
{
foreach($items as $item) {
$this->addItem($item);
}
return $this;
}
/**
* addItem
*
* Add a sigle collection item
*
* @param EntityInterface $entity The entity to add to the
* element collection
*/
public function addItem(EntityInterface $item)
{
$element = $this->createNewItem($item->getId());
$this->get($this->getCollectionName())->add($element);
}
/**
* createNewItem
*
* Create a new collection item
*
* @param EntityInterface $entity The entity to create
* @return \Zend\Form\ElementInterface
*/
protected function createNewItem($id, array $options = array())
{
$element = clone $this->targetElement;
$element->setOptions(array_merge($element->getOptions(), $options));
$element->setCheckedValue($id);
return $element;
}
然后需要做的就是从控制器操作中将集合传递给表单
SomeController
public function listAction()
{
//....
$users = $objectManager->getRepository('user')->findBy(array('foo' => 'bar'));
$form = $this->getServiceLocator()->get('my_list_form');
$form->addItems($users);
//...
}
您可以使用数据库中的条令,使用
DoctrineModule\Form\Element\ObjectMultiCheckbox
填充多选复选框,如本页所示:
只需将实体管理器传递给表单,然后执行与创建ObjectMultiCheckbox表单元素示例中相同的操作
或者另一种更好的-moro自动化工作方法,如果你想使用集合,你需要对区域进行正确的映射(@orm\OneToMany和@orm\ManyToOne)。。。然后创建一个字段集,格式如下…:
并将方法添加到其他实体以添加和删除区域,如下所示:
public function addArea(Collection $areas)
{
foreach ($areas as $area) {
$area->setOtherEntity($this);
$this->areas->add($area);
}
}
public function removeAreas(Collection $areas)
{
foreach ($areas as $area) {
$area->setOtherEntity(null);
$this->areas->removeElement($area);
}
}
这样,如果您使用水合作用,则会在自动选择值时添加和删除这些值
public function listAction()
{
//....
$users = $objectManager->getRepository('user')->findBy(array('foo' => 'bar'));
$form = $this->getServiceLocator()->get('my_list_form');
$form->addItems($users);
//...
}
public function addArea(Collection $areas)
{
foreach ($areas as $area) {
$area->setOtherEntity($this);
$this->areas->add($area);
}
}
public function removeAreas(Collection $areas)
{
foreach ($areas as $area) {
$area->setOtherEntity(null);
$this->areas->removeElement($area);
}
}