我有一个名为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');
我在这里尝试实现的是一份注册课程的用户列表,课程数量,以及可能的已完成课程数量。
不幸的是,doctrine似乎只能加入到用户表或课程表中,而不是两者都加入。这甚至可能是一个mysql限制。我一遍又一遍地调试了这个问题 - 我用不同的例子多次遇到问题 - 我似乎无法找到除了使用->from(Tracking)
之外的解决方案消除那些没有开始任何课程的学生,以及他们尚未开始的课程的统计数据。我一遍又一遍地用Google搜索,但是很难找到这个问题而没有得到如何使用Doctrine&#39;来加入两个表格。
我收到错误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(Security\Credential::class, 'c');
$q->from(Security\SecurableCourse::class, 'sc');
$q->from(Security\AccreditableInheritance::class, 'ai');
$q->from(Security\AccreditableUser::class, 'au');
$q->from(User::class, 'user');
$q->join(Tracking\CourseTracking::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):执行&#39; 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_.completed)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)AND t2_.dtype IN(&#39; coursetracking&#39;)AND((t2_.deleted IS NULL或t2_.deleted&gt;&#39; 2016-04- 26 08:33:31&#39;)),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)AND(c3_.action =?AND s1_.course_id IN(?,?,?)))AND((u0_.deleted IS NULL或u0_.deleted&gt;&#39; 2016- 04-26 08:33:31&#39;)))和(s1_.dtype IN(&#39; securablecourse&#39;)和a5_.dtype IN(&#39; accreditableuser&#3 9;))GROUP BY u0_.id&#39;用params [\&#34; study&#34;,\&#34; 46 \&#34;,\&#34; 45 \&#34;,\&#34; 160 \&#34; ]:\ n \ nSQLSTATE [42S22]:未找到列:1054未知列&#39; s1_.id&#39;在&#39; on条款&#39;
答案 0 :(得分:1)
这只是提示如何实现它。我不能给你正确答案,因为你没有提供足够的细节。但这将帮助您实现所需。
$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');