X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FCoreBundle%2FEntity%2FTag.php;h=4b480ff1cdc4f2c4ebc2b7e4bf4b558edb95eb9a;hb=5ecdfcd041767c9e3244a92bb0a6cc3c3f80fea3;hp=6f005314f69fd8e33a2e253a0d135bf74913171c;hpb=8ce32af61229eec8f4cc34b207273d47f60adc48;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/CoreBundle/Entity/Tag.php b/src/Wallabag/CoreBundle/Entity/Tag.php index 6f005314..4b480ff1 100644 --- a/src/Wallabag/CoreBundle/Entity/Tag.php +++ b/src/Wallabag/CoreBundle/Entity/Tag.php @@ -2,17 +2,18 @@ namespace Wallabag\CoreBundle\Entity; +use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; -use JMS\Serializer\Annotation\XmlRoot; use JMS\Serializer\Annotation\ExclusionPolicy; use JMS\Serializer\Annotation\Expose; -use Doctrine\Common\Collections\ArrayCollection; +use Gedmo\Mapping\Annotation as Gedmo; +use JMS\Serializer\Annotation\XmlRoot; /** * Tag. * * @XmlRoot("tag") - * @ORM\Table + * @ORM\Table(name="`tag`") * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\TagRepository") * @ExclusionPolicy("all") */ @@ -37,18 +38,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="User", inversedBy="tags") + * @ORM\ManyToMany(targetEntity="Entry", mappedBy="tags", cascade={"persist"}) */ - private $user; + private $entries; - public function __construct(User $user) + public function __construct() { - $this->user = $user; $this->entries = new ArrayCollection(); } @@ -91,16 +93,61 @@ class Tag return $this->label; } + public function getSlug() + { + return $this->slug; + } + + /** + * @param Entry $entry + */ public function addEntry(Entry $entry) { - $this->entries[] = $entry; + if ($this->entries->contains($entry)) { + return; + } + + $this->entries->add($entry); + $entry->addTag($this); } /** - * @return User + * @param Entry $entry */ - public function getUser() + public function removeEntry(Entry $entry) { - return $this->user; + if (!$this->entries->contains($entry)) { + return; + } + + $this->entries->removeElement($entry); + $entry->removeTag($this); + } + + public function hasEntry($entry) + { + return $this->entries->contains($entry); + } + + /** + * Get entries for this tag. + * + * @return ArrayCollection + */ + public function getEntries() + { + 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; } }