2

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();
...

助けてくれてありがとう。

4

3 に答える 3

2

Samから提供されたurl docのおかげで、フォーム生成、フォーム バインド前に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);

これで、エンティティ セレクタのドロップダウン リストがアルファベット順に並べられました。どうもありがとうサム!

于 2013-06-12T09:48:58.713 に答える
0

これは私のために働いた

 * @Annotation\Options({"label":"Team(s):", "find_method"={"name": "findBy", "params"={"criteria"={}, "orderBy"={"name":"ASC"}}}})*
于 2014-03-25T08:30:27.520 に答える
-1

プロパティの上のエンティティに追加します

@ORM\OrderBy({"名前" = "ASC"})

詳細はこちら

編集

もちろん、名前とASCは、好み/ニーズの値に置き換える必要があります

于 2013-06-11T20:38:13.923 に答える