Propel 选择条件中的 IF 语句
我正在尝试将 IF 语句纳入我的推进标准中。 这就是我建立标准的方式:
$c = is_object($c) ? $c : new Criteria();
$c->addSelectColumn("CONCAT(DAYOFYEAR(" . SomePeer::CREATED_AT . "), ' ', YEAR(" . SomePeer::CREATED_AT . ")) AS period");
$c->addSelectColumn("COUNT(" . SomePeer::ID . ") AS registrations");
$c->addSelectColumn("COUNT(IF(" . SomePeer::JOIN . " > 0 AND " . SomePeer::JOIN . " < 3, 1, NULL)) AS coreg");
$c->addJoin(SomePeer::IDENTIFIER, self::TRACKING_IDENTIFIER);
$c->add(SomePeer::ASSOCIATE_ID, $user->getId());
$c->addJoin(self::USER_ID, SomePeer::ID);
$c->addJoin(SomePeer::ID, SomePeer::USER_ID);
$c->add(SomePeer::CREDENTIAL, 'student');
$c->addJoin(SomePeer::PROFILE_ID, SomePeer::ID);
但是,当检查生成的 SQL 时,我得到以下信息:
SELECT CONCAT(MONTHNAME(some_table.CREATED_AT), ' ', YEAR(some_table.CREATED_AT)) AS period, COUNT(some_table.ID) AS registrations, COUNT(IF(some_table.JOIN > 0 AND some_table.JOIN < 3, 1, NULL)) AS coreg
FROM `some_table`, `>` `0` `AND` `some_table`, `some_table`, `some_table`, `some_table`, `some_table`
WHERE some_table.ASSOCIATE_ID=:p1
AND some_table.CREDENTIAL=:p2
AND some_table.IDENTIFIER=some_table.TRACKING_IDENTIFIER
AND some_table.STUDENT_USER_ID=some_table.ID
AND some_table.ID=some_table.USER_ID
AND some_table.PROFILE_ID=some_table.ID
# Params: some_table.ASSOCIATE_ID => 6, some_table.CREDENTIAL => 'credential'"
基本上,如您所见,查询的 FROM 部分都是错误的。我猜 propel 将 COUNT 中的 IF 语句的一部分误认为是表名,并将它们包含在查询的 FROM 部分中。 有谁知道我如何解决这个问题,或者有人甚至能够让一些东西发挥作用?
感谢您的帮助,
文森特
I am trying to incorporate an IF statement into my propel criteria.
This is the how I build up my criteria:
$c = is_object($c) ? $c : new Criteria();
$c->addSelectColumn("CONCAT(DAYOFYEAR(" . SomePeer::CREATED_AT . "), ' ', YEAR(" . SomePeer::CREATED_AT . ")) AS period");
$c->addSelectColumn("COUNT(" . SomePeer::ID . ") AS registrations");
$c->addSelectColumn("COUNT(IF(" . SomePeer::JOIN . " > 0 AND " . SomePeer::JOIN . " < 3, 1, NULL)) AS coreg");
$c->addJoin(SomePeer::IDENTIFIER, self::TRACKING_IDENTIFIER);
$c->add(SomePeer::ASSOCIATE_ID, $user->getId());
$c->addJoin(self::USER_ID, SomePeer::ID);
$c->addJoin(SomePeer::ID, SomePeer::USER_ID);
$c->add(SomePeer::CREDENTIAL, 'student');
$c->addJoin(SomePeer::PROFILE_ID, SomePeer::ID);
However when inspecting the resulting SQL I get the following:
SELECT CONCAT(MONTHNAME(some_table.CREATED_AT), ' ', YEAR(some_table.CREATED_AT)) AS period, COUNT(some_table.ID) AS registrations, COUNT(IF(some_table.JOIN > 0 AND some_table.JOIN < 3, 1, NULL)) AS coreg
FROM `some_table`, `>` `0` `AND` `some_table`, `some_table`, `some_table`, `some_table`, `some_table`
WHERE some_table.ASSOCIATE_ID=:p1
AND some_table.CREDENTIAL=:p2
AND some_table.IDENTIFIER=some_table.TRACKING_IDENTIFIER
AND some_table.STUDENT_USER_ID=some_table.ID
AND some_table.ID=some_table.USER_ID
AND some_table.PROFILE_ID=some_table.ID
# Params: some_table.ASSOCIATE_ID => 6, some_table.CREDENTIAL => 'credential'"
Basically, as you can see, the FROM part of the query is all wrong. I guess propel mistook part of the IF statement in the COUNT as table names and included them in the FROM part of the query.
Has anyone got any ideas how I could get 'round this problem or has someone even been able to get something to work?
Thanks for all your help,
Vincent
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题似乎出在 JOIN 名称上。您是否使用“join”作为字段名称???在那里尝试一个不同的名称。
Seems like the problem is with the name JOIN. Are you using "join" as a field name??? Try a different name there.