简体   繁体   中英

Adding tag field from Sonata ClassificationBundle to your (custom) entity

I'm trying out the ClassificationBundle from the Sonata project in order to add a tag field to my entity type 'Project'.

However, I find the documentation on how to actually do this very lacking. There appears only to be an integration example in the SonataNewsBundle (as this was the original project this bundle was intended for).

Based on that news bundle I figured some things I had to do, such as:

1) Add fields to my AppBundle\\Entity\\Project class :

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) Add form field to my AppBundle\\Admin\\ProjectAdmin class :

   protected function configureFormFields(FormMapper $formMapper) {
     ..

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

But if I then browse to the admin form (/admin/project/create)

The current field tags is not linked to an admin. Please create one for the target entity: ``

In my AppBundle/Resources/config/admin.yml I have this :

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]]

When I use 'entity' as my field type, with a reference to the generated Tag entity, it works

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

Any help would be appreciated.

I presume somewhere I have to say Sonata to use the ClassificationBundle for that field, but as said documentation seems to lack, which is something not quite unusual for the Sonata project.

Declare your servise like this :

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]]

You are missing the tag join table definition. In the Sonata News Bundle it's in DependencyInjection/SonataNewsExtension.php . But you can just add it to your entity as an annotation, something like:

/**
     * @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;

And then run app/console doctrine:schema:update --force .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM