学说限制打破了我的疑问?

时间:2013-04-16 22:39:12

标签: mysql symfony-1.4 doctrine-1.2

我有以下代码:

$params = array();
$query_questions_visibles = questionTable::getInstance()->createQuery("q")
    ->select("q.*, ua.*, a.*, u.*, c.*")
    ->leftJoin("q.Answers a")
    ->leftJoin("a.UserAnswers ua")
    ->Where("q.blocked = false")
    ->andWhere("ua.user_id = :user_id")
->orderBy("q.id DESC")
->groupBy("q.id");

    //Subquery --> Calculates when a question is active or not      
    $format = sfConfig::get("app_datetime_format");
    $active_time = date($format, strtotime(sfConfig::get("app_question_active_time")));
    $sub_query_is_active = $query_questions_visibles->createSubQuery()
            ->select("MAX(ua0.created_at)")
            ->from("question q0")
            ->leftJoin("q0.Answers a0")
            ->leftJoin("a0.UserAnswers ua0")
            ->where("q0.id = q.id");                
    $query_questions_visibles->addSelect("COALESCE((".$sub_query_is_active.") > :active_time, false) as Active");

//Set param values
$params["user_id"] = $guardUser->id;
$params["active_time"] = $active_time;

$result = $query_questions_visibles->execute($params);

以前的代码按预期工作。

生成的SQL已完成且有效:

  

SELECT q.id AS q__id,q.user_id AS q__user_id,q.category_id AS q__category_id,q.gender_restriction AS q__gender_restriction,q.question AS q_ question,q.photo AS q _photo,q .latitude AS Q_ 纬度,q.longitude为q _longitude,q.multiple AS Q_ 多个,q.blocked为q _blocked,q.created_at AS q__created_at,q.updated_at AS q__updated_at,A.ID AS a__id,a.question_id AS a__question_id,a.text AS A_ 文本,u.id为u _id,u.user_id AS u__user_id,u.answer_id AS u__answer_id,U。 created_at AS u__created_at,u.updated_at AS u__updated_at,COALESCE((SELECT MAX(u2.created_at)AS u2__0 FROM问题Q2 LEFT JOIN应答A2 ON q2.id = a2.question_id LEFT JOIN user_answer U2 ON a2.id = u2.answer_id WHERE (q2.id = q.id))>:active_time,0)AS u2__0 FROM question q LEFT JOIN answer a ON q.id = a.question_id LEFT JOIN user_answer u ON a.id = u.answer_id WHERE(q。 blocked = 0 AND u.user_id =:user_id)GROUP BY q.id ORDER BY q.id DESC

但是,如果我想限制结果,我将结束行修改为:

$query_questions_visibles->limit(10);
$result = $query_questions_visibles->execute($params);

Doctrine抛出错误:

  

SQLSTATE [HY093]:参数号无效:绑定变量数与令牌数不匹配

     

在Doctrine_Connection->执行('SELECT DISTINCT q2.id FROM问题Q2 LEFT JOIN应答A2 ON q2.id = a2.question_id LEFT JOIN user_answer U2 ON a2.id = u2.answer_id WHERE q2.blocked = 0 AND u2.user_id =:user_id GROUP BY q2.id ORDER BY q2.id DESC LIMIT 10',array('user_id'=>'1','active_time'=>'2013-03-17 17:12:12 '))在SF_ROOT_DIR \ lib \ vendor \ symfony \ lib \ plugins \ sfDoctrinePlugin \ lib \ vendor \ doctrine \ Doctrine \ Query.php第1290行......

我的目的只是将查询限制为10个结果,¿我的带有COALESCE和MAX函数的Subquery在哪里? ¿为什么有一个我从未指定的SELECT DISTINCT? ¿为什么只选择q.id?

我花了一整天的时间试图弄明白,我没有回答...... 设置限制的任何想法会导致这种情况吗?

1 个答案:

答案 0 :(得分:1)

当您向Doctrine查询添加limit()时,Doctrine内部实际上会创建两个查询。第一个根据查询条件选择一组有限的不同ID。第二个查询选择实际对象,将select限制为使用第一个查询找到的ID。

您的查询的问题是您在select部分内部使用了params,而第一个查询中没有使用它。

我想到的唯一解决方法是将active_time参数的值直接添加到选择部分,而不使用命名参数。它可能不是最好的解决方案,但我现在想不出一个不同的解决方案。 addSelect()不接受where()之类的其他参数可以解决问题(您可以使用where()->where('field > ?', $value))。