使用zf2和 Doctrine ,我正在为我的Salesman类生成一个表单。 推销员对商店有一个ManyToOne参考(即商店可以有一个或多个推销员)
由于我使用 @Annotation \ Type(“DoctrineORMModule \ Form \ Element \ EntitySelect”),我在表单中显示了一个下拉列表,这正是我想要的。
我想要实现的是在@ManyToOne关联框架中根据商店名称属性对商店进行分类
以下是我所拥有的HTML(生成)代码:
<select>
<option value="1" selected="selected">Store A</option>
<option value="2">Store C</option> <- not ordered! probably because using row id for sorting.
<option value="3">Store B</option>
<select>
这就是我想要的:
<select>
<option value="1" selected="selected">Store A</option>
<option value="3">Store B</option>
<option value="2">Store C</option> <- good, now my store are alphabetically ordered :-)
<select>
@Annotation \ Type(“DoctrineORMModule \ Form \ Element \ EntitySelect”)接受 @ORM \ OrderBy({“name”=“ASC”})可选注释但这仅适用于@OneToMany或@ManyToMany: - (
问题:
如何使用 @ManyToOne关联在 EntitySelect 中进行排序?
PHP源代码摘录:
<?php
namespace Customer\Entity;
use Doctrine\ORM\Mapping as ORM;
use Zend\Form\Annotation;
/**
* @ORM\Entity
*/
class Salesman extends AbstractEntity
{
...
/**
* @ORM\ManyToOne(targetEntity="Customer\Entity\Store", fetch="EAGER")
* @Annotation\Attributes({"readonly":"false"})
* @Annotation\Type("DoctrineORMModule\Form\Element\EntitySelect")
* @Annotation\Options({"label":"Store:", "target_class":"Customer\Entity\Store"})
*/
protected $store;
...
}
<?php
namespace Customer\Entity;
use Doctrine\ORM\Mapping as ORM;
use Zend\Form\Annotation;
/**
* @ORM\Entity
*/
class Store extends AbstractEntity
{
...
/**
* @ORM\Column(type="string", length=100)
* @Annotation\Options({"label":"Name: "})
*/
protected $name;
/**
* @ORM\OneToMany(targetEntity="Customer\Entity\Salesman", mappedBy="store", cascade={"all"}, orphanRemoval=true)
* @Annotation\Attributes({"type":"hidden"})
* @Annotation\Required(false)
* @Annotation\Type("Zend\Form\Element\Collection")
* @Annotation\Options({
* "label" : "Salesmen",
* "target_element" : {
* "composedObject" : "Customer\Entity\Salesman"
* }
* })
*/
protected $salesmen;
...
}
关于视图(.phtml),没什么特别要提的:只是基本形式。
...
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formCollection($form);
echo $this->form()->closeTag();
...
感谢您的帮助。
答案 0 :(得分:2)
感谢url doc提供的Sam,我可以弄清楚如何在表单生成和之前表单中更改我的EntitySelect 的选项结合。
我是这样做的:
$storeOptions = $this->form->get('store')->getOptions();
/* mannually changing options.
If someone knows how to achieve this using annotations, I am interested :-) */
$storeOptions['is_method'] = true;
$storeOptions['find_method'] = array(
'name' => 'findBy',
'params' => array(
'criteria' => array(), // no criteria since I want the whole list
'orderBy' => array('name' => 'ASC'),
),
);
$this->form->get('store')->setOptions($storeOptions);
现在我的实体选择器下拉列表按字母顺序排列。非常感谢Sam!
答案 1 :(得分:0)
这对我有用
* @Annotation\Options({"label":"Team(s):", "find_method"={"name": "findBy", "params"={"criteria"={}, "orderBy"={"name":"ASC"}}}})*
答案 2 :(得分:-1)