X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=sidebyside;f=src%2FWallabag%2FCoreBundle%2FEntity%2FTag.php;h=9fb2f94fddcf8216e879f80d6e27e5064315d198;hb=8668796106b856ca041512af27268ce6e49d2caf;hp=7cc452fd7f97b860f4eabadeaf9523ec95e4e5bd;hpb=bd0f3d32c9ccb8f7a1409edb960b909a5e6a096d;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/CoreBundle/Entity/Tag.php b/src/Wallabag/CoreBundle/Entity/Tag.php index 7cc452fd..9fb2f94f 100644 --- a/src/Wallabag/CoreBundle/Entity/Tag.php +++ b/src/Wallabag/CoreBundle/Entity/Tag.php @@ -2,17 +2,24 @@ namespace Wallabag\CoreBundle\Entity; +use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; -use JMS\Serializer\Annotation\XmlRoot; +use Gedmo\Mapping\Annotation as Gedmo; use JMS\Serializer\Annotation\ExclusionPolicy; use JMS\Serializer\Annotation\Expose; -use Doctrine\Common\Collections\ArrayCollection; +use JMS\Serializer\Annotation\XmlRoot; /** * Tag. * * @XmlRoot("tag") - * @ORM\Table(name="`tag`") + * @ORM\Table( + * name="`tag`", + * options={"collate"="utf8mb4_bin", "charset"="utf8mb4"}, + * indexes={ + * @ORM\Index(name="tag_label", columns={"label"}, options={"lengths"={255}}), + * } + * ) * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\TagRepository") * @ExclusionPolicy("all") */ @@ -37,18 +44,19 @@ class Tag private $label; /** - * @ORM\ManyToMany(targetEntity="Entry", mappedBy="tags", cascade={"persist"}) + * @Expose + * @Gedmo\Slug(fields={"label"}) + * @ORM\Column(length=128, unique=true) */ - private $entries; + private $slug; /** - * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="tags") + * @ORM\ManyToMany(targetEntity="Entry", mappedBy="tags", cascade={"persist"}) */ - private $user; + private $entries; - public function __construct(\Wallabag\UserBundle\Entity\User $user) + public function __construct() { - $this->user = $user; $this->entries = new ArrayCollection(); } @@ -76,7 +84,7 @@ class Tag */ public function setLabel($label) { - $this->label = $label; + $this->label = mb_convert_case($label, MB_CASE_LOWER); return $this; } @@ -91,9 +99,29 @@ class Tag return $this->label; } + public function getSlug() + { + return $this->slug; + } + public function addEntry(Entry $entry) { - $this->entries[] = $entry; + if ($this->entries->contains($entry)) { + return; + } + + $this->entries->add($entry); + $entry->addTag($this); + } + + public function removeEntry(Entry $entry) + { + if (!$this->entries->contains($entry)) { + return; + } + + $this->entries->removeElement($entry); + $entry->removeTag($this); } public function hasEntry($entry) @@ -102,10 +130,24 @@ class Tag } /** - * @return User + * Get entries for this tag. + * + * @return ArrayCollection */ - public function getUser() + public function getEntries() { - return $this->user; + return $this->entries; + } + + public function getEntriesByUserId($userId) + { + $filteredEntries = new ArrayCollection(); + foreach ($this->entries as $entry) { + if ($entry->getUser()->getId() === $userId) { + $filteredEntries->add($entry); + } + } + + return $filteredEntries; } }