1

I have category table and make table. Both tables related by third category_make table creating many to many relationship.

/**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="Ladisi\MotorsBundle\Entity\Make", inversedBy="catogory", fetch="EAGER")
     * @ORM\JoinTable(name="catogory_make",
     *   joinColumns={
     *     @ORM\JoinColumn(name="catogory_id", referencedColumnName="cat_id")
     *   },
     *   inverseJoinColumns={
     *     @ORM\JoinColumn(name="make_id", referencedColumnName="make_id")
     *   }
     * )
     */
    private $make;

I want to get makes that belongs to particular category. I have tried,

        $query = $em
            ->createQuery(
                'SELECT c, m FROM LadisiMotorsBundle:Catagory c
                JOIN c.make m
                WHERE c.catId= :id'
            )->setParameter('id', $id);
        $result = $query->getResult();

but every time I get only category fields, make entity is not available in result. I also tried to get makes just by calling getMakes method on catagory object, it also returns null (not entity, i guess proxy). How do i solve this. Any help would be great.

Ruwanka De Silva
  • 3,555
  • 6
  • 35
  • 51

1 Answers1

0

You've already created a linking table, so the only thing you have to do (if you have your entities configured correctly) in your controller:

$catagory = $this->getDoctrine()->getManager()->getRepository('LadisiMotorsBundle:Catagory')->findOneBy(['id' => $id]);
$catagory->getMakes();
priktop
  • 1,155
  • 3
  • 12
  • 30
  • As I mentioned, I have tried your method it returned null, that's why i posted the question. – Ruwanka De Silva Dec 16 '14 at 15:20
  • Can you show the code in your Catagory entity? I see that you use catogory and catagory inconsistently. That might be something to look at. – priktop Dec 16 '14 at 16:08
  • I know there are some typos, I have fixed issue by using `$result = $query->getResult(Query::HYDRATE_ARRAY);` on posted code. I have forgotten that I was able to find a solution, thanks any way. But I don't know what is going here, if you don't mind can you explain why your approach is not working (I have tried it again, with considering typos). Is there something related with fetching method? – Ruwanka De Silva Dec 16 '14 at 16:53
  • 1
    Ah yes, hydration :) With hydration you can specify the way how your results are loaded from the database (see http://stackoverflow.com/questions/2661762/what-is-doctrine-hydration for a better explanation). You probably expected an array where you want to use your Makes instead of an object or something. Anyway, glad to see that you figured it out. – priktop Dec 16 '14 at 17:04