2

I've this two entities:

class Article
{
    /** @var ArrayCollection
    protected $tags;
}

class Tag
{
    protected $id;
    protected $name;
}

Basically I've read http://symfony.com/doc/master/cookbook/form/form_collections.html but it does not explain my case.

Let me explain.

I've existing tags in database, so I want to be able to associate multiple Tags to my Article, I don't want to allow the creation of inexistent tags.

Since I'm aiming to use my form in both a REST api and with a web form, I'd like my client be able to use the id only to reference the tags (which would have been fetched before).

Any ideas?

1
  • How did you solved your problem? Commented Nov 27, 2012 at 18:36

2 Answers 2

1

You could use the entity field type. It is in practice a choice field type that you can render as a select with multiple selection or as a list of checkboxes. However, if you have many tags there will be too many options and the users will not be happy.

So, I suggest to implement a Stackoverflow-like tagging system.

I did it in a project by using a Javascript tokeninput library, like this by loopj. In practice:

  1. You should first render a text field named tags.

  2. Then you should handle the tag insertion to that input field with the Javascript library for token handling.

  3. The Controller will receive a tokenized string that you have to handle in order to retrieve the Tag entities from Doctrine.

  4. Finally, retrieved a list of Tag entities, assign them to your Article entity and flush everything.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use somethig like this:

$builder->add('tags', 'entity', array(
    'class' => 'AcmeHelloBundle:tag',
    'expanded' => true,
    'multiple' => true,
));

1 Comment

Yes, as I suggested! However, it is not useful in practice when you have dozens of tags! Imagine the amount of tags in Stackoverflow shown in a unique select!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.