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 Gedmo\Mapping\Annotation as Gedmo;
+use JMS\Serializer\Annotation\XmlRoot;
/**
* Tag.
private $label;
/**
+ * @Expose
* @Gedmo\Slug(fields={"label"})
* @ORM\Column(length=128, unique=true)
*/
*/
private $entries;
- /**
- * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="tags")
- */
- private $user;
-
- public function __construct(\Wallabag\UserBundle\Entity\User $user)
+ public function __construct()
{
- $this->user = $user;
$this->entries = new ArrayCollection();
}
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);
+ }
+
+ /**
+ * @param Entry $entry
+ */
+ public function removeEntry(Entry $entry)
+ {
+ if (!$this->entries->contains($entry)) {
+ return;
+ }
+
+ $this->entries->removeElement($entry);
+ $entry->removeTag($this);
}
public function hasEntry($entry)
}
/**
- * @return User
+ * Get entries for this tag.
+ *
+ * @return ArrayCollection<Entry>
*/
- public function getUser()
+ public function getEntries()
+ {
+ return $this->entries;
+ }
+
+ public function getEntriesByUserId($userId)
{
- return $this->user;
+ $filteredEntries = new ArrayCollection();
+ foreach ($this->entries as $entry) {
+ if ($entry->getUser()->getId() === $userId) {
+ $filteredEntries->add($entry);
+ }
+ }
+
+ return $filteredEntries;
}
}