1

学校エンティティと学生エンティティの間に多対 1 の関係を設定しました。manyToOne 関係を使用して 2 つをリンクしました。

学生エンティティ:

/**
 * @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 を指定する Students テーブルのクエリを作成しようとしていますが、クエリ内でこれを指定する方法がわかりません。

$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));

学生 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. 
4

0 に答える 0