Php 通过注释生成器在Zend Framework 2表单中填充关系数据
目前,我有文章和标签表。我正在尝试将“Tag”表单元素自动填充为文章表单上的选择框。从数据库表中设置标记选择框的值选项,然后让文章在“bind”方法调用期间自动绑定标记数据的最佳方法是什么 Article.phpPhp 通过注释生成器在Zend Framework 2表单中填充关系数据,php,doctrine-orm,zend-framework2,zend-form,zend-form-element,Php,Doctrine Orm,Zend Framework2,Zend Form,Zend Form Element,目前,我有文章和标签表。我正在尝试将“Tag”表单元素自动填充为文章表单上的选择框。从数据库表中设置标记选择框的值选项,然后让文章在“bind”方法调用期间自动绑定标记数据的最佳方法是什么 Article.php <?php // Article class class Article { /** * * @var \Doctrine\Common\Collections\Collection|Tag[] * * @ORM\ManyToM
<?php
// Article class
class Article {
/**
*
* @var \Doctrine\Common\Collections\Collection|Tag[]
*
* @ORM\ManyToMany(targetEntity="Tag", inversedBy="articles")
* @Orm\JoinTable(name="rel_article_tag", joinColumns={@ORM\JoinColumn(name="article_id", referencedColumnName="article_id")}, inverseJoinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="tag_id")})
*
* @Form\Required(false)
* @Form\Type("Zend\Form\Element\Select")
* @Form\Options({"label":"Tags: ")
* @Form\Attributes({"id":"tags", "data-placeholder":"Choose tags...", "multiple" : "multiple", "class" : "chosen-select"})
*/
private $tags;
public function __construct()
{
$this->tags = new ArrayCollection();
}
public function getTags()
{
return $this->tags;
}
public function addTags($tags)
{
$this->tags = $tags;
}
public function removeTags()
{
$this->tags = new ArrayCollection();
}
}
class ArticleController{
public function editAction()
{
$builder = new AnnotationBuilder();
$form = $builder->createForm(new TblArticle());
$id = 1;
$article = $em->find('Admin\Entity\TblArticle', $id);
$form->bind($article);
}
}
我所做的
在ArticleController::editAction()
中,我已将值选项动态添加到表单上的tags元素中
class ArticleController
{
public function editAction()
{
$builder = new AnnotationBuilder();
$form = $builder->createForm(new TblArticle());
// add tag options to form
$sm = $this->getServiceLocator();
$em = $sm->get('Doctrine\ORM\EntityManager');
$tags = $em->getRepository('Admin\Entity\LuTag')->findAll();
$tagOptions = [null => ''];
foreach ($tags as $tag) {
$tagOptions[$tag->getTagId()] = $tag->getName();
}
$form->get('tags')->setValueOptions($tagOptions);
// end add tag options to form
$id = 1;
$article = $em->find('Admin\Entity\TblArticle', $id);
$form->bind($article);
if ($article->getTags()) {
$tagIds = array();
foreach ($article->getTags() as $tag) {
$tagIds[] = $tag->getTagId();
}
$form->get('tags')->setValue($tagIds);
}
}
}
我的控制器中似乎有过多的代码,我知道这是不对的,但我不知道如何更好地做到这一点。是否可能使用FormBuilder为标记元素设置值选项
谢谢。查看本教程:
基本上,您需要在标记实体注释中指定存储库类,如下所示:
@ORM\Entity(repositoryClass="Admin\Entity\LuTag")
然后,您可以使用Doctrins DoctrineModule\Form\Element\ObjectSelect类型,该类型将能够提供您请求的功能:
Article.php(注意@Form\Type注释和其他@Form\Options条目)
也请查看
有关ObjectSelect的详细信息
最后,您需要使用
DoctrineORMModule\Form\Annotation\AnnotationBuilder
而不是Zends AnnotationBuilder,以解决对象与管理器的依赖关系
/* using the service manager like this within a controller method is
bad practice. Inject the EntityManager using a Controller Factory! */
$sm = $this->getServiceLocator();
$em = $sm->get('Doctrine\ORM\EntityManager');
$builder = new DoctrineORMModule\Form\Annotation\AnnotationBuilder($em);
$form = $builder->createForm(TblArticle::class);
这应该能奏效。我认为这是不可能的。
/* using the service manager like this within a controller method is
bad practice. Inject the EntityManager using a Controller Factory! */
$sm = $this->getServiceLocator();
$em = $sm->get('Doctrine\ORM\EntityManager');
$builder = new DoctrineORMModule\Form\Annotation\AnnotationBuilder($em);
$form = $builder->createForm(TblArticle::class);