将Sonata ClassificationBundle中的标记字段添加到您的(自定义)实体

时间:2014-12-21 12:12:36

标签: symfony tags classification categories sonata-admin

我在Sonata项目中尝试ClassificationBundle,以便为我的实体类型添加标记字段' Project'。

但是,我发现有关如何实际执行此操作的文档非常缺乏。 在SonataNewsBundle中似乎只有一个集成示例(因为这是该捆绑包的原始项目)。

基于该新闻包,我想到了一些我必须做的事情,例如:

1)将字段添加到我的AppBundle \ Entity \ Project类:

private $tags;

/**
 * Add tags
 *
 * @param \Sonata\ClassificationBundle\Model\TagInterface $tags
 */
public function addTags(TagInterface $tags)
{
    $this->tags[] = $tags;
}

/**
 * Get tags
 *
 * @return array $tags
 */
public function getTags()
{
    return $this->tags;
}

/**
 * @param $tags
 *
 * @return mixed
 */
public function setTags($tags)
{
    $this->tags = $tags;
}

2)将表单字段添加到我的AppBundle \ Admin \ ProjectAdmin类:

   protected function configureFormFields(FormMapper $formMapper) {
     ..

     ->add('tags', 'sonata_type_model_autocomplete', array(
          'property' => 'name',
          'multiple' => 'true'
        ))

但是如果我然后浏览到管理表单(/ admin / project / create)

  

当前字段tags未链接到管理员。请为目标实体创建一个:``

在我的AppBundle / Resources / config / admin.yml中我有这个:

services:
  sonata.admin.Project:
    class: AppBundle\Admin\ProjectAdmin
    tags:
        - { name: sonata.admin, manager_type: orm, group: "Content", label: "Project" }
    arguments:
        - ~
        - AppBundle\Entity\Project
        - ~
    calls:
        - [ setTranslationDomain, [AppBundle]]

当我使用' entity'作为我的字段类型,通过引用生成的Tag实体,它可以正常工作

->add('tags', 'entity', array(
        'property' => 'name',
        'multiple' => 'true',
        'class' => 'Application\Sonata\ClassificationBundle\Entity\Tag',
      ))

任何帮助都将不胜感激。

我假设某个地方我不得不说Sonata在该字段中使用ClassificationBundle,但正如所述文档似乎缺乏,这对Sonata项目来说并不常见。

2 个答案:

答案 0 :(得分:0)

声明您的服务:

services:
sonata.admin.Project:
  class: AppBundle\Admin\ProjectAdmin
  tags:
       - { name: sonata.admin, manager_type: orm, group: "Content", label: "Project" }
  arguments:  [~, AppBundle\Entity\Project, AppBundle:ProjectAdmin]
  calls:
      - [ setTranslationDomain, [AppBundle]]

答案 1 :(得分:0)

您缺少标记连接表定义。在Sonata News Bundle中,它位于DependencyInjection/SonataNewsExtension.php。但您可以将它作为注释添加到您的实体,例如:

    /**
     * @ORM\ManyToMany(targetEntity="Application\Sonata\ClassificationBundle\Entity\Tag", cascade={"persist"})
     * @ORM\JoinTable(
     *      name="Project_tag",
     *      joinColumns={@ORM\JoinColumn(name="project_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="id")}
     * )
     */
    protected $tags;

然后运行app/console doctrine:schema:update --force