如何在 DQL 查询中访问相关实体的属性(原则 2)?

发布于 2024-11-30 03:46:02 字数 1700 浏览 1 评论 0原文

我在学校实体和学生实体之间建立了多对一关系,并使用多对一关系将两者链接在一起;

学生实体:

/**
 * @var \Doctrine\Common\Collections\Collection
 * @ManyToOne(targetEntity="Schools", inversedBy="Students")
 * @JoinColumn(name="school_id", referencedColumnName="school_id")
 */
private $Schools;

学校实体:

/**
 * @var \Doctrine\Common\Collections\Collection
 * @OneToMany(targetEntity="Students", mappedBy="Schools")
 */
private $Students;

这按预期工作。

我正在尝试为 Students 表编写一个查询,该表指定 school_id,但我不知道如何在查询中指定它;

$qb->select('t.position')
    ->from('\Entities\Students', 't')
    ->where($qb->expr()->eq('t.studentId', ':student_id'))
    ->andWhere($qb->expr()->eq('t.Schools.schoolId', ':school_id')) // doesn't work
    ->setParameters(array('student_id' => $this->studentId, 'school_id' => $school_id));

我尝试通过在学生身份中设置的 $Schools 属性访问学校 ID,但这会引发错误。

作为临时修复,我在 Students 实体中创建了一个 $schoolId 属性,并使用它来代替,但这肯定不是执行此操作的正确方法。

感谢您的帮助。

更新

我通过改变线路让它工作;

->andWhere($qb->expr()->eq('t.Schools.schoolId', ':school_id')) // doesn't work

收件人:

->andWhere($qb->expr()->eq('t.Schools', ':school_id'))

但是,相同的方法不适用于以下查询:

$qb->select('DISTINCT t.age, s.Schools') // Doesn't work
    ->from('\Entities\Schools', 't')
    ->innerJoin('t.Students', 's')

显示以下错误:

[Semantical Error] line 0, col 28 near 'Schools FROM': Error: Invalid PathExpression. Must be a StateFieldPathExpression. 

I have set up a many to one relationship between a schools entity and students entity, I've linked the two together using a manyToOne relationship;

Students entity:

/**
 * @var \Doctrine\Common\Collections\Collection
 * @ManyToOne(targetEntity="Schools", inversedBy="Students")
 * @JoinColumn(name="school_id", referencedColumnName="school_id")
 */
private $Schools;

Schools entity:

/**
 * @var \Doctrine\Common\Collections\Collection
 * @OneToMany(targetEntity="Students", mappedBy="Schools")
 */
private $Students;

This works as expected.

I am trying to write a query for the Students table which specifies the school_id but I don't know how to specify this within the query;

$qb->select('t.position')
    ->from('\Entities\Students', 't')
    ->where($qb->expr()->eq('t.studentId', ':student_id'))
    ->andWhere($qb->expr()->eq('t.Schools.schoolId', ':school_id')) // doesn't work
    ->setParameters(array('student_id' => $this->studentId, 'school_id' => $school_id));

I've attempted to access the school id through $Schools property I set up in the student identity but this throws an error.

As a temporary fix I've created a $schoolId property in the Students entity and am using this instead but surely this is not the correct way to do this.

Appreciate the help.

UPDATE

I got it to work by changing the line;

->andWhere($qb->expr()->eq('t.Schools.schoolId', ':school_id')) // doesn't work

To:

->andWhere($qb->expr()->eq('t.Schools', ':school_id'))

However the same approach doesn't work with the following query:

$qb->select('DISTINCT t.age, s.Schools') // Doesn't work
    ->from('\Entities\Schools', 't')
    ->innerJoin('t.Students', 's')

The following error is displayed:

[Semantical Error] line 0, col 28 near 'Schools FROM': Error: Invalid PathExpression. Must be a StateFieldPathExpression. 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文