Skip to content

Commit e533bd0

Browse files
committed
minor #450 Cleanup and comments about Tags feature implementation (yceruto, javiereguiluz)
This PR was merged into the master branch. Discussion ---------- Cleanup and comments about Tags feature implementation * Removed unused old `isAuthor()` method from `Post` * Removed useless PHPDoc * Add code comments about Tags feature implementation * Removed setByReference option which is unnecessary now. Commits ------- 6147bbd Updated the last help note a62876f Updated some help notes 4d71179 Updated a help note a574d24 Remove byReference option which is unnecessary now ba34c26 Cleanup and comments about Tags feature implementation
2 parents 6ef7bb0 + 6147bbd commit e533bd0

File tree

5 files changed

+20
-15
lines changed

5 files changed

+20
-15
lines changed

src/AppBundle/Entity/Post.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,6 @@ public function setAuthor(User $author)
186186
$this->author = $author;
187187
}
188188

189-
/**
190-
* Is the given User the author of this Post?
191-
*/
192-
public function isAuthor(User $user = null)
193-
{
194-
return $user === $this->author;
195-
}
196-
197189
public function getComments()
198190
{
199191
return $this->comments;

src/AppBundle/Entity/Tag.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ public function getName()
5555
*/
5656
public function jsonSerialize()
5757
{
58+
// This entity implements JsonSerializable (http://php.net/manual/en/class.jsonserializable.php)
59+
// so this method is used to customize its JSON representation when json_encode()
60+
// is called, for example in tags|json_encode (app/Resources/views/form/fields.html.twig)
61+
5862
return $this->name;
5963
}
6064

src/AppBundle/Form/DataTransformer/TagArrayToStringTransformer.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public function __construct(ObjectManager $manager)
3737
*/
3838
public function transform($array)
3939
{
40+
// The value received is an array of Tag objects generated with
41+
// Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer::transform()
42+
// The value returned is a string that concatenates the string representation of those objects
43+
4044
/* @var Tag[] $array */
4145
return implode(',', $array);
4246
}
@@ -52,17 +56,22 @@ public function reverseTransform($string)
5256

5357
$names = explode(',', $string);
5458

59+
// Get the current tags and find the new ones that should be created.
5560
$tags = $this->manager->getRepository(Tag::class)->findBy([
5661
'name' => $names,
5762
]);
58-
5963
$newNames = array_diff($names, $tags);
6064
foreach ($newNames as $name) {
6165
$tag = new Tag();
6266
$tag->setName($name);
6367
$tags[] = $tag;
68+
69+
// There's no need to persist these new tags because Doctrine does that automatically
70+
// thanks to the cascade={"persist"} option in the AppBundle\Entity\Post::$tags property.
6471
}
6572

73+
// Return an array of tags to transform them back into a Doctrine Collection.
74+
// See Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer::reverseTransform()
6675
return $tags;
6776
}
6877
}

src/AppBundle/Form/Type/DateTimePickerType.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@
2828
*/
2929
class DateTimePickerType extends AbstractType
3030
{
31-
/**
32-
* @var MomentFormatConverter
33-
*/
3431
private $formatConverter;
3532

3633
public function __construct()

src/AppBundle/Form/Type/TagsInputType.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,12 @@ public function __construct(ObjectManager $manager)
4444
public function buildForm(FormBuilderInterface $builder, array $options)
4545
{
4646
$builder
47-
->setByReference(false)
48-
->addModelTransformer(new TagArrayToStringTransformer($this->manager))
49-
->addModelTransformer(new CollectionToArrayTransformer())
47+
// The Tag collection must be transformed into a comma separated string.
48+
// We could create a custom transformer to do Collection <-> string in one step,
49+
// but here we're doing the transformation in two steps (Collection <-> array <-> string)
50+
// and reuse the existing CollectionToArrayTransformer.
51+
->addModelTransformer(new CollectionToArrayTransformer(), true)
52+
->addModelTransformer(new TagArrayToStringTransformer($this->manager), true)
5053
;
5154
}
5255

0 commit comments

Comments
 (0)