2

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.

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({"name" = "ASC"})

更多信息在这里

编辑

当然名称和 ASC 需要替换为您喜欢/需要的值

于 2013-06-11T20:38:13.923 回答