我有一个名为tracking
的实体。它涉及User
和Course
。我还有一个名为credential
的实体,它正式链接User
和Course
User <- Tracking -> Course
|| User <- Credential -> Course
跟踪是某种连接实体,但它不是两者之间的主要连接。我有一个查询,其中我已经加入了用户和课程,并且我想左加入跟踪(如果存在)。我试图简化我的例子。
$q->select(
'user.id',
'user.firstname',
'user.lastname',
'count(t) as courses',
'count(t.completed) as completed'
);
$q->from(Credential::class, 'c');
$q->from(Course::class, 'course');
$q->from(User::class, 'user');
$q->leftJoin(Tracking::class, 't', 'WITH', 't.user = user and t.course = course');
$q->where('c.user = user and c.object = course');
$q->groupBy('user');
我在这里要实现的是注册课程的用户列表、课程数量以及可能的情况下已完成课程的数量。
不幸的是,学说似乎只能加入用户表或课程表,而不能同时加入两者。这甚至可能是 mysql 的限制。我已经一遍又一遍地调试了这个 - 我已经用不同的示例多次遇到这个问题 - 除了使用->from(Tracking)
之外,我似乎找不到解决方案,这将消除尚未开始任何课程的学生,以及他们尚未开始的课程的统计数据。我一遍又一遍地用谷歌搜索,但是很难搜索这个问题而没有得到"如何使用教义连接两个表"。
我收到错误Column not found: 1054 Unknown column 'c1_.id' in 'on clause'
我认为这意味着它可以加入t.user = user
但不能t.course = course
这是实际代码和错误
$q = $this->em->createQueryBuilder();
$q->select(
'user.id',
'user.firstname',
'user.lastname',
'count(sc.id) as courses',
'count(ct.commenced) as commenced',
'count(ct.completed) as completed',
'avg(ct.scorePercent) as avgscore',
'avg(ct.totalTime) as avgtime'
);
$q->from(SecurityCredential::class, 'c');
$q->from(SecuritySecurableCourse::class, 'sc');
$q->from(SecurityAccreditableInheritance::class, 'ai');
$q->from(SecurityAccreditableUser::class, 'au');
$q->from(User::class, 'user');
$q->join(TrackingCourseTracking::class, 'ct', 'WITH', 'ct.objectIdentity = sc and ct.user = user');
$q->where('sc = c.securable and ai.parent = c.accreditable and au = ai.child and user = au.user');
$q->andWhere('c.action = :action and sc.course in (:courses)');
$q->setParameter('action', 'study')->setParameter('courses', $courses);
$q->groupBy('user.id');
$users = $q->getQuery()->getScalarResult();
Doctrine\DBAL\Exception\InvalidFieldNameException(code: 0):执行 'SELECT u0_.id AS id_0、u0_.firstname AS firstname_1、u0_.lastname AS lastname_2、count(s1_.id) AS sclr_3、count(t2_.commenced) AS sclr_4、count(t2_.complete) AS sclr_5、avg(t2_.scorePercent) AS sclr_6、avg(t2_.totalTime) AS sclr_7 FROM Credential c3_ INNER JOIN TRACKING t2_ ON (t2_.objectIdentity_id = s1_.id AND t2_.user_id = u0_ 时发生异常。ID) 和 t2_.DTYPE IN ('COURSETRACKING') 和 ((t2_.DELETE 为 NULL 或 t2_.delete> '2016-04-26 08:33:31'))、SecurableIdentity s1_、AccreditableInheritance a4_、AccreditableIdentity a5_、User u0_ WHERE (((s1_.id = c3_.securable_id AND a4_.parent_id = c3_.accreditable_id AND a5_.id = a4_.child_id AND u0_.id = a5_.user_id) 和 (c3_.action = ?和 s1_.course_id IN (?, ?, ?)))和 ((u0_.DELETE 为 NULL 或 u0_.DELETE> '2016-04-26 08:33:31'))) 和 (s1_.dtype IN ('SECURABLECOURSE') 和 a5_.dtype IN ('ACCREDITABLEUSER')) 按 u0_.ID' 分组,带有参数 [\"study\", \"46\", \"45\", \"160\"]:\SQLSTATE[42S22]:找不到列:1054 "on 子句"中的未知列"s1_.id"
这只是如何实现它的提示。我不能给你正确的答案,因为你没有提供足够的细节。但这将帮助您实现所需的目标。
$q->select(u, t, co, ce);
$q->from('User', 'u');
$q->leftJoin('u.tracking', 't');
$q->leftJoin('t.course', 'co');
$q->leftJoin('u.credential', 'ce');