我在这里和谷歌上搜索了几乎所有我找到的东西,但仍然一无所获。
我有一个与国家有ManytoMany关系的用户实体,如下所示:
/**
* @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表单元素?
$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
)
));非常感谢你的帮助,我真的很感激!
发布于 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/
您基本上创建了一个自定义存储库,其中包含一个自定义查询以检索可能的解决方案:
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中引用该方法:
$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',
),
),
));https://stackoverflow.com/questions/22957003
复制相似问题