与共生和教义的关系问题


Relation issue with symfony and doctrine

我有3个实体,"讲座","库尔斯"和"课程图像"。

在课堂上讲课我有:

/**
 * @ORM'ManyToOne(targetEntity="Course", cascade={"persist"})
 * @ORM'JoinColumn(name="courseId", referencedColumnName="courseId")
 */
protected $course;

在课堂上 课程 我有:

/**
 * @ORM'OneToMany(targetEntity="CourseImage", mappedBy="course")
 */
protected $images;

所有getter/setter都是由symfony正确生成的。

我制作了一个自定义存储库"LectureRepository"来检索一些带有本机查询的讲座,其中我的代码是:

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();
}

最后在我的控制器中,我有这个:

$popularLectures = $this
    ->getDoctrine()
    ->getManager()
    ->getRepository('AcmeDemoBundle:Lecture')
    ->findPopularByLocation($latitude, $longitude);

这正确地检索了我的讲座,我也能够从讲座中检索课程:

$popularLectures[0]->getCourse();

但是当我尝试时:

$popularLectures[0]->getCourse()->getImages();

我没有得到任何结果..我想我应该在本机查询中添加一些东西,使其也从数据库中检索图像,但我不确定如何,我的研究没有给出任何回报:(任何帮助将不胜感激!

编辑:课程中的获取图像如下。

/**
 * Get images
 *
 * @return 'Doctrine'Common'Collections'Collection
 */
public function getImages()
{
    return $this->images;
}

发现问题,我没有 inversedBy 参数。无论如何,感谢您的评论。

/**
 * @ORM'ManyToOne(targetEntity="Course", inversedBy="images", cascade={"persist"})
 * @ORM'JoinColumn(name="courseId", referencedColumnName="courseId")
 */
protected $course;