[x] Add a way to add Tags in the form Article.
[x] Link a Tag to an Article if he already exists.
[x] Tags are represented as text with space between other Tags.
[x] Tests dataTransformer.
- Create an entity
Articlewith those properties :
- title (string)
- author (string)
- content (text)
- createdAt (datetime)
- Create another entity Tag with a title. Which has a ManyToMany relationship with the Article entity.
- Create a
TagRepositoryclass with the methodpersistAndFlush, doing basically a persist then a flush of an object "Tag". - Make it as a service: create a
doctrine.xmlfile in/Resources/configand declare your repository as a service withfactory-service="doctrine.orm.entity_manager"&&factory-method="getRepository". - Load your
doctrine.xmlin the Extension file of your bundle (/src/YourBundle/DependencyInjection). - Create a ModelTransformer class (
TagModelTransformer) which implements theDataTransformerInterfaceinForm\DataTransformer\. - Pass to its constructor the
TagRepository - Implements the
transformandreverseTransformmethods for converting an arrayCollection to a simple array and conversely. - In
reverseTransformmethod if a tag doesn't exist, persist a new one with ObjectManager. If not, link to Article. - Declare the
TagModelTransformeras a private service (public="false"). - Create a ViewTransformer class (
TagViewTransformer) which implements theDataTransformerInterfaceinForm\DataTransformer\. - Implements
transformandreverseTransformmethod for converting an array to a string with space and conversely : You can useexplodeandimplodePHP functions. - Declare it as a private service.
- Now time to create a Type for the tag called
TagTypewhich implements the methodgetName()(mandatory, returning a string), and the methodbuildForm(taking as arguments a FormBuilderInterface && an array of options). - Add the
TagModelTransformer&&TagViewTransformerto the builder. - You'll make it a service: you'll need your two DataTransformers, so create 2 private properties that will be initiated in the constructor.
- Declare it as service: create a
form.xmlfile in/Resources/configand declare your type with the tagform.type(make sure that the alias is the same as the string returned by thegetName()method in your type), the two dependancies => your ModelTransformer and ViewTransformer$builder->addViewTransformer($viewTransformer)->addModelTransformer($modelTransformer); - Then create a Type for your Article called
ArticleTypewith your new TagType (indicate the alias of your type like->add('tags', 'tags_selector')). - Declare it as a service in the
form.xmlfile. - Then create your Controller with actions to show, create, edit, delete articles.
- Create views accordingly to your actions.
- Time for tests! Create a directory /Tests/Unit/Form/DataTransformer
- To test the
TagModelTransformerTest:- first create a
TagModelTransformerTestextending\PHPUnit_Framework_TestCase; - implement the
setUp()method to initiate the two private properties$repo(a mock of theTagRepository) and$transformer(a real instance ofTagModelTransformertaking the repository as argument) - create your unit tests to test the
transform()andreverseTransform()methods of your ModelTransformer => basically verify that the method converts an arrayCollection to a simple array and conversely. - Make sure that your tests cover the case of having an existing tag title and a new tag title!
- first create a
- To test the
TagViewTransformerTest:- first create a
TagViewTransformerTestextending\PHPUnit_Framework_TestCase; - create your unit tests to test the
transform()andreverseTransform()methods of your ViewTransformer => basically verify that the method converts an array to a string with space and conversely.
- first create a
Et voilà !
Some documentation:
- http://symfony.com/doc/current/cookbook/form/data_transformers.html
- https://speakerdeck.com/webmozart/3-steps-to-symfony2-form-mastery
- https://speakerdeck.com/hhamon/ingenierie-inversee-du-composant-form-de-symfony