1

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

学生单位:

/**
 * @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;

这按预期工作。

我正在尝试为指定 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,但这会引发错误。

作为临时修复,我在 Student 实体中创建了一个 $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. 
4

0 回答 0