简体   繁体   English

教义问题-不同的查询,结果相同,但教义不同

[英]Doctrine issue - Different queries, same results but not with Doctrine

i'm having a little issue with doctrine using symfony 1.4 (I think it's using doctrine 1.2). 我在使用symfony 1.4的教义上有一点问题(我认为这是在使用教义1.2)。 I have 2 queries, using raw sql in the mysql console, they produce the same resultset. 我有2个查询,在mysql控制台中使用原始sql,它们产生相同的结果集。 The queries can be generated using this code : 可以使用以下代码生成查询:

    $dates = Doctrine::getTable('Picture')
              ->createQuery('a')
              ->select('substr(a.created_at,1,10) as date')
              ->leftjoin('a.PictureTag pt ON a.id = pt.picture_id')
              ->leftjoin('pt.Tag t ON t.id = pt.tag_id')
              ->where('a.created_at <= ?', date('Y-m-d 23:59:59'))
              ->orderBy('date DESC')
              ->groupby('date')
              ->limit(ITEMS_PER_PAGE)
              ->offset(ITEMS_PER_PAGE * $this->page)
              ->execute();

If I remove the two joins, it changes the query, but the resultset it's the same. 如果删除两个联接,它将更改查询,但结果集相同。 But using doctrine execute(), one produces only one row. 但是使用原则execute()时,只产生一行。

Somebody have an idea on what's going on here? 有人对这里发生的事情有想法吗?

PS : Picture table has id, title, file, created_at (format 'Ymd h:i:s'), the Tag table is id, name and PictureTag is an relationship table with id and the two foreign keys. PS:图片表具有ID,标题,文件,created_at(格式为'Ymd h:i:s'),Tag表为ID,名称和PictureTag是具有ID和两个外键的关系表。

PS 2 : Here are the two sql queries produced (the first without joins) PS 2:这是产生的两个sql查询(第一个不包含联接)

SELECT substr(l.created_at, 1, 10) AS l__0 FROM lupa_picture l WHERE (l.created_at <= '2010-03-19 23:59:59') GROUP BY l__0 ORDER BY l__0 DESC LIMIT 4

 SELECT substr(l.created_at, 1, 10) AS l__0 FROM lupa_picture l LEFT JOIN lupa_picture_tag l2 ON (l.id = l2.picture_id) LEFT JOIN lupa_tag l3 ON (l3.id = l2.tag_id) WHERE (l.created_at <= '2010-03-19 23:59:59') GROUP BY l__0 ORDER BY l__0 DESC LIMIT 4

I had something similar this week. 这周我有类似的事情。 Doctrine's generated SQL (from the Symfony debug toolbar) worked fine in phpMyAdmin, but failed when running the query as in your question. Doctrine生成的SQL(通过Symfony调试工具栏)在phpMyAdmin中运行良好,但是在运行查询时却出现问题,失败了。 Try adding in the following into your query: 尝试在查询中添加以下内容:

->setHydrationMode(Doctrine::HYDRATE_SCALAR)

and see if it gives you the expected result. 看看是否能给您预期的结果。 If so, it's down to the Doctrine_Collection using the Picture primary key as the index in the collection. 如果是这样,则使用Picture主键作为集合中的索引归结为Doctrine_Collection。 If you have more than 1 result with the same index, Doctrine will refuse to add it into the collection, so you only end up with 1 result. 如果您有多个具有相同索引的结果,Doctrine将拒绝将其添加到集合中,因此最终只会得到1个结果。 I ended up running the query using a different table rather than the one I wanted, which resulted in a unique primary key and then the results I wanted appeared. 我最终使用不同的表而不是想要的表运行查询,这导致了唯一的主键,然后出现了想要的结果。

Well, the solution was that...besides substr(), it needs another column of the table. 好吧,解决方案是...除了substr()之外,还需要表的另一列。 Using select(substr(), a.created_at) made it work 使用select(substr(),a.created_at)使其工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM