目前,我有文章和标签表.我试图自动填充"标签"表单元素作为文章表单上的选择框.从数据库表中设置标签选择框的值选项的最佳方法是什么,然后让文章在"绑定"方法调用期间自动绑定标签数据?
Article.php
<?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();
}
}
Run Code Online (Sandbox Code Playgroud)
ArticleController.php …
php zend-form zend-form-element doctrine-orm zend-framework2