relation between tags and entries

This commit is contained in:
Nicolas Lœuillet 2015-02-24 07:42:09 +01:00
parent 6c87418ff0
commit 46bbd8d321
7 changed files with 60 additions and 148 deletions

View file

@ -6,6 +6,7 @@ use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\Tag;
class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface
{
@ -37,6 +38,14 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface
$entry3->setTitle('test title entry3');
$entry3->setContent('This is my content /o/');
$tag1 = new Tag();
$tag1->setLabel("foo");
$tag2 = new Tag();
$tag2->setLabel("bar");
$entry3->addTag($tag1);
$entry3->addTag($tag2);
$manager->persist($entry3);
$this->addReference('entry3', $entry3);

View file

@ -1,61 +0,0 @@
<?php
namespace Wallabag\CoreBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Wallabag\CoreBundle\Entity\Tag;
use Wallabag\CoreBundle\Entity\TagsEntries;
class LoadTagData extends AbstractFixture implements OrderedFixtureInterface
{
/**
* {@inheritDoc}
*/
public function load(ObjectManager $manager)
{
$tag1 = new Tag();
$tag1->setLabel('foo');
$manager->persist($tag1);
$this->addReference('tag1', $tag1);
$tagsEntries1 = new TagsEntries();
$tagsEntries1->setEntryId($this->getReference('entry1'));
$manager->persist($tagsEntries1);
$tag2 = new Tag();
$tag2->setLabel('bar');
$manager->persist($tag2);
$this->addReference('tag2', $tag2);
$tagsEntries2 = new TagsEntries();
$tagsEntries2->setEntryId($this->getReference('entry2'));
$manager->persist($tagsEntries2);
$tag3 = new Tag();
$tag3->setLabel('baz');
$manager->persist($tag3);
$this->addReference('tag3', $tag3);
$tagsEntries3 = new TagsEntries();
$tagsEntries3->setEntryId($this->getReference('entry2'));
$manager->persist($tagsEntries3);
$manager->flush();
}
/**
* {@inheritDoc}
*/
public function getOrder()
{
return 30;
}
}

View file

@ -120,11 +120,8 @@ class Entry
private $user;
/**
* @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist", "merge"})
* @ORM\JoinTable(name="tags_entries",
* joinColumns={@ORM\JoinColumn(name="entry_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="id")}
* )
* @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"})
* @ORM\JoinTable(name="entry_tags")
*/
private $tags;
@ -407,5 +404,6 @@ class Entry
public function addTag(Tag $tag)
{
$this->tags[] = $tag;
$tag->addEntry($this);
}
}

View file

@ -6,6 +6,7 @@ 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;
/**
* Tag
@ -36,10 +37,14 @@ class Tag
private $label;
/**
* @ORM\ManyToMany(targetEntity="Entry", mappedBy="tags", cascade={"persist", "merge"})
* @ORM\ManyToMany(targetEntity="Entry", mappedBy="tags", cascade={"persist"})
*/
private $entries;
public function __construct()
{
$this->entries = new ArrayCollection();
}
/**
* Get id
*
@ -72,4 +77,9 @@ class Tag
{
return $this->label;
}
public function addEntry(Entry $entry)
{
$this->entries[] = $entry;
}
}

View file

@ -1,81 +0,0 @@
<?php
namespace Wallabag\CoreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* TagsEntries
*
* @ORM\Table(name="tags_entries")
*/
class TagsEntries
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*
* @ORM\ManyToOne(targetEntity="Entry", inversedBy="tags_entries")
* @ORM\JoinColumn(name="entry_id", referencedColumnName="id")
*
*/
private $entryId;
/**
*
* @ORM\ManyToOne(targetEntity="Tag", inversedBy="tags_entries")
* @ORM\JoinColumn(name="tag_id", referencedColumnName="id")
*
*/
private $tagId;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @return mixed
*/
public function getEntryId()
{
return $this->entryId;
}
/**
* @param mixed $entryId
*/
public function setEntryId($entryId)
{
$this->entryId = $entryId;
}
/**
* @return mixed
*/
public function getTagId()
{
return $this->tagId;
}
/**
* @param mixed $tagId
*/
public function setTagId($tagId)
{
$this->tagId = $tagId;
}
}

View file

@ -118,4 +118,15 @@ class EntryRepository extends EntityRepository
->getQuery()
->getResult();
}
public function findOneWithTags()
{
$qb = $this->createQueryBuilder('e')
->innerJoin('e.tags', 't')
->addSelect('t');
return $qb
->getQuery()
->getOneOrNullResult();
}
}

View file

@ -150,4 +150,30 @@ class WallabagRestControllerTest extends WallabagTestCase
$this->assertEquals(404, $client->getResponse()->getStatusCode());
}
public function testGetTagsEntry()
{
$client = $this->createClient();
$client->request('GET', '/api/salts/admin.json');
$salt = json_decode($client->getResponse()->getContent());
$headers = $this->generateHeaders('admin', 'test', $salt[0]);
$entry = $client->getContainer()
->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry')
->findOneWithTags();
if (!$entry) {
$this->markTestSkipped('No content found in db.');
}
$tags = array();
foreach ($entry->getTags() as $tag) {
$tags[] = array('id' => $tag->getId(), 'label' => $tag->getLabel());
}
$client->request('GET', '/api/entries/'.$entry->getId().'/tags', array(), array(), $headers);
$this->assertEquals(json_encode($tags), $client->getResponse()->getContent());
}
}