Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions src/AppBundle/Entity/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,6 @@ public function setAuthor(User $author)
$this->author = $author;
}

/**
* Is the given User the author of this Post?
*/
public function isAuthor(User $user = null)
{
return $user === $this->author;
}

public function getComments()
{
return $this->comments;
Expand Down
4 changes: 4 additions & 0 deletions src/AppBundle/Entity/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public function getName()
*/
public function jsonSerialize()
{
// This entity implements JsonSerializable (http://php.net/manual/en/class.jsonserializable.php)
// so this method is used to customize its JSON representation when json_encode()
// is called, for example in tags|json_encode (app/Resources/views/form/fields.html.twig)

return $this->name;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public function __construct(ObjectManager $manager)
*/
public function transform($array)
{
// The value received is an array of Tag objects generated with
// Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer::transform()
// The value returned is a string that concatenates the string representation of those objects

/* @var Tag[] $array */
return implode(',', $array);
}
Expand All @@ -52,17 +56,22 @@ public function reverseTransform($string)

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

// Get the current tags and find the new ones that should be created.
$tags = $this->manager->getRepository(Tag::class)->findBy([
'name' => $names,
]);

$newNames = array_diff($names, $tags);
foreach ($newNames as $name) {
$tag = new Tag();
$tag->setName($name);
$tags[] = $tag;

// There's no need to persist these new tags because Doctrine does that automatically
// thanks to the cascade={"persist"} option in the AppBundle\Entity\Post::$tags property.
}

// Return an array of tags to transform them back into a Doctrine Collection.
// See Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer::reverseTransform()
return $tags;
}
}
3 changes: 0 additions & 3 deletions src/AppBundle/Form/Type/DateTimePickerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
*/
class DateTimePickerType extends AbstractType
{
/**
* @var MomentFormatConverter
*/
private $formatConverter;

public function __construct()
Expand Down
9 changes: 6 additions & 3 deletions src/AppBundle/Form/Type/TagsInputType.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ public function __construct(ObjectManager $manager)
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->setByReference(false)
->addModelTransformer(new TagArrayToStringTransformer($this->manager))
->addModelTransformer(new CollectionToArrayTransformer())
// The Tag collection must be transformed into a comma separated string.
// We could create a custom transformer to do Collection <-> string in one step,
// but here we're doing the transformation in two steps (Collection <-> array <-> string)
// and reuse the existing CollectionToArrayTransformer.
->addModelTransformer(new CollectionToArrayTransformer(), true)
->addModelTransformer(new TagArrayToStringTransformer($this->manager), true)
;
}

Expand Down