Using zf2 and Doctrine, I am generating a form for my Salesman class. Salesman has a ManyToOne reference to Store (i.e., a store can have 1 or more salesmen)
Since I use @Annotation\Type("DoctrineORMModule\Form\Element\EntitySelect"), I have a drop-down list displayed in the form, which is exactly what I want.
What I would like to achieve, is sorting stores based on store's name property in the frame of a @ManyToOne association
Here is the HTML (generated) code that I have:
<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>
And here is what I want:
<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") accept @ORM\OrderBy({"name" = "ASC"}) optional annotation but this only works for @OneToMany or @ManyToMany :-(
Question:
How can I achieve ordering in my EntitySelect with my @ManyToOne association?
PHP source code excerpts:
<?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;
...
}
About the view (.phtml), nothing special to mention: just basic form.
...
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formCollection($form);
echo $this->form()->closeTag();
...
Thanks for helping.