I have 3 entities, "Lecture", "Cource" and "CourseImage".
In class Lecture I have:
/**
* @ORM\ManyToOne(targetEntity="Course", cascade={"persist"})
* @ORM\JoinColumn(name="courseId", referencedColumnName="courseId")
*/
protected $course;
And in class Course I have:
/**
* @ORM\OneToMany(targetEntity="CourseImage", mappedBy="course")
*/
protected $images;
All getters/setters are generated by symfony correctly.
I made a custom repository "LectureRepository" to retrieve some lectures with a native query where my code is:
public function findPopularByLocation($latitude, $longitude)
$rsm = new ResultSetMapping();
$rsm->addEntityResult('Acme\DemoBundle\Entity\Lecture', 'l');
$rsm->addFieldResult('l', 'lectureId', 'lectureId');
$rsm->addFieldResult('l', 'latitude', 'latitude');
$rsm->addFieldResult('l', 'longitude', 'longitude');
$rsm->addFieldResult('l', 'start', 'start');
$rsm->addMetaResult('l', 'courseId', 'courseId', true);
$sql = 'SELECT * , ( 3959 * ACOS( COS( RADIANS( ? ) ) * COS( RADIANS( latitude ) ) * COS( RADIANS( longitude ) - RADIANS( ? ) ) + SIN( RADIANS( ? ) ) * SIN( RADIANS( latitude ) ) ) ) AS distance FROM lectures HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;';
$query = $this->getEntityManager()->createNativeQuery($sql, $rsm);
$query->setParameter(1, $latitude);
$query->setParameter(2, $longitude);
$query->setParameter(3, $latitude);
return $query->getResult();
}
Finally in my controller I have this:
$popularLectures = $this
->getDoctrine()
->getManager()
->getRepository('AcmeDemoBundle:Lecture')
->findPopularByLocation($latitude, $longitude);
Which retrieves correctly my lectures, and I am able to also retrieve the course from the lecture:
$popularLectures[0]->getCourse();
But when I try:
$popularLectures[0]->getCourse()->getImages();
I get no results.. I suppose I should add something in the native query to make it retrieve images as well from the database, but I am not sure how and my research gave nothing back :( Any help will be appreciated!
EDIT: The getImages in Course is as follows.
/**
* Get images
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getImages()
{
return $this->images;
}
Found the issue, I didn't have the inversedBy parameter in there..Thanks for the comments anyway.
/**
* @ORM\ManyToOne(targetEntity="Course", inversedBy="images", cascade={"persist"})
* @ORM\JoinColumn(name="courseId", referencedColumnName="courseId")
*/
protected $course;