symfonydoctrine-ormdoctrinedoctrine-1.2doctrine-query

Doctrine Many To Many Inner Join


I'm trying to get manyToMany relationship with Doctrine and symfony. I'm new at Doctrine and Symfony. I came from Zend framework.

I've created the 3 tables: Post, PostCategory and junction table PostToCategory as you can see below.

Tables example

My goal is to do inner join and to get for every post its categories. This is what I've done so far:

//CMS/GBundle/Entity/PostContent

class PostContent
{

    /**
     * @ORM\ManyToMany(targetEntity="CMS\GBundle\Entity\PostCategory", inversedBy="posts")
     * @ORM\JoinTable(name="post_to_category")
     */
    protected $categories;

    public function __construct()
    {
        $this->categories = new ArrayCollection();
    }

//CMS/GBundle/Entity/PostCategory

class PostCategory
{

    /**
     * @ORM\ManyToMany(targetEntity="CMS\GBundle\Entity\PostContent", mappedBy="categories")
     */
    protected $posts;

    public function __construct()
    {
        $this->posts = new ArrayCollection();
    }

I would like now to create function that returns me joined data Post->PostCategories.

Where should I create function ? in PostToCategory Repository ? or somewhere else ? How should my query look like ?

I've tried a lot of options and I passed all the possible questions on Stack but I could not get it done..

Thanks in advance!

Update:

This is what i get when do findAll

This is what i get when i do findAll method on PostContent repository.


Solution

  • The preferred way to make a relationship is to create your entities like you did, then run a console command to extend your entity with some getters and setters:

    $ bin/console doctrine:generate:entities AppBundle
    

    and then let doctrine create your tables:

    $ bin/console doctrine:schema:update --force
    

    Now that is everything ready you have to fill some data in your database tables.

    $category = new Category();
    $categroy->setName('PHP Programming');
    $em->persist($category);
    
    $post = new Post();
    $post->setTitle('My first Blogpost');
    $post->addCategory($category);
    $em->persist($post);
    
    $em->flush();
    

    After that can get it out. I just give you an example

    public function indexAction($id)
    {
        $em = $this->getDoctrine()->getManager();
        $category= $em->getRepository('AppBundle:PostCategory')->find($id);
    
        // test
        foreach($category->getPosts() as $post)
        {
            echo $post->getTitle() . '<br>';
        }
    }
    

    To load all the posts immediately instead of lazy loading i suggest to write your own query in the CategoryRepository class and change the find() method-name for your own method-name.