zf2 とDoctrineを使用して、Salesman クラスのフォームを生成しています。セールスマンは Store への ManyToOne 参照を持っています (つまり、ストアには 1 人以上のセールスマンがいる可能性があります)。
@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();
...
助けてくれてありがとう。