首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将ArrayCollection传递给DoctrineModule\Form\Element\ObjectSelect

如何将ArrayCollection传递给DoctrineModule\Form\Element\ObjectSelect
EN

Stack Overflow用户
提问于 2014-04-09 16:34:32
回答 1查看 1K关注 0票数 0

我在这里和谷歌上搜索了几乎所有我找到的东西,但仍然一无所获。

我有一个与国家有ManytoMany关系的用户实体,如下所示:

代码语言:javascript
复制
/**
 * @var \Doctrine\Common\Collections\Collection
 * @ORM\ManyToMany(targetEntity="Admin\Entity\Country", cascade={"persist", "remove"})
 * @ORM\JoinTable(name="user_country_linker",
 *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="country_id", referencedColumnName="id")}
 * )
 */
protected $countries;

现在,我正在尝试显示DoctrineModule\Form\Element\ObjectSelect with allowed/ assigned countries。我确实可以通过调用$this->zfcUserAuthentication()->getIdentity()->getCountries().获得这个列表

有没有办法把这个ArrayCollection传递给ObjectSelect表单元素?

代码语言:javascript
复制
$this->add(array(
        'name' => 'country',
        'type' => 'DoctrineModule\Form\Element\ObjectSelect',
        'options' => array(
            'label'             => 'Country',
            'object_manager'    => $em,
            'target_class'      => '\Admin\Entity\Country',
            'property'          => 'code',
            'find_method'       => array(
                'name' => 'findBy',
                'params' => array(
                    'criteria' => array(),
                    'orderBy' => array('id' => 'asc'),
                ),
            ),
            'column-size' => 'sm-10',
            'label_attributes' => array('class' => 'col-sm-2'),
            'help-block' => 'Select country where the entity is present'
        ),
        'attributes' => array(
            'required' => false
        )
    ));

非常感谢你的帮助,我真的很感激!

EN

回答 1

Stack Overflow用户

发布于 2014-08-11 01:21:05

如何在你的控制器中填充下拉列表在这里有最好的描述:zf2 create select/drop down box and populate options in controller?。这基本上是AlexP的解决方案。

如果这不是你想要的,也许这篇文章描述的方法可以帮助你。至少它可以帮助像我这样寻找解决方案的其他人:http://samsonasik.wordpress.com/2014/05/22/zend-framework-2-using-doctrinemoduleformelementobjectselect-and-custom-repository/

您基本上创建了一个自定义存储库,其中包含一个自定义查询以检索可能的解决方案:

代码语言:javascript
复制
namespace Your\Repository;


    use Doctrine\ORM\EntityRepository;

    class CountriesRepository extends EntityRepository
    {
        public function getPossibleCountries()
        {
            $querybuilder = $this->_em
                                 ->getRepository($this->getEntityName())
                                 ->createQueryBuilder('c');
            return $querybuilder->select('c')//... define your query here
                        ->getQuery()->getResult();
        }
    }

然后,您可以在ObjectSelect中引用该方法:

代码语言:javascript
复制
            $this->add(array(
               'name' => 'continent',
               'type' => 'DoctrineModule\Form\Element\ObjectSelect',
               'options' => array(
                    'object_manager'     => $this->entityManager,
                    'target_class'       => 'Your\Entity\User',
                    'property' => 'contries',
                    'is_method' => true,
                    'find_method'        => array(
                        'name'   => 'getCountries',
                    ),
                ), 
            ));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22957003

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档