0

我试图弄清楚如何使用 Yii 的 CDbCriteria 构建查询,该查询等效于以下内容:

 SELECT 
        * 
FROM 
        user u 
JOIN ( 
        SELECT * 
        FROM skill_assessment s 
        WHERE s.skill = 'HTML' 
        AND s.score >= 80 
) b ON 
        (u.id = b.userId) 
JOIN ( 
        SELECT * 
        FROM skill_assessment s 
        WHERE s.skill = 'CSS3' 
        AND s.score >= 80
) c ON 
        (u.id = c.userId);
etc...

这是我到目前为止所拥有的,这是行不通的:

$criteria = new CDbCriteria();
$criteria->alias = "u";
$criteria->select = "*";
$criteria->join = "JOIN skill_assessment s ON (u.id=s.userId)";
for($i = 0; $i < count($skill_filters); $i++) {
    $criteria->addCondition("s.skill='".$skill_filters[$i]->skill."' AND s.score >= ".$skill_filters[$i]->level);
}

$users = UserModel::model()->findAll($criteria);

任何帮助将不胜感激。提前致谢。

编辑:我能够将 sql 查询构建为字符串并使用 findAllBySql,它返回了与我的搜索条件匹配的正确 UserModel,问题是我无法让它返回相关的 SkillAssessmentModel。他们也不会返回初始查询,如下所示:

$users = UserModel::model()->with('skill_assessments')->findAllBySql($sql);

如果我得到这样的结果:

$users = UserModel::model->findAllBySql($sql);
foreach($users as $user)
{
     $user->skill_assessments = $user->getRelated('skill_assessments');
}

关于如何获得这些相关模型的任何想法?奇怪的是,在我的应用程序的其他地方,如果我这样做,我可以获得相关模型:

$user = UserModel::model->findByPk($id);
$user->skill_assessments->getRelated('skill_assessments');
4

3 回答 3

2

addCondition默认情况下,第二个参数是'AND'。这是你想要的?可能您应该定义并在'OR'条件$operator周围添加大括号。但在您的情况下addCondition,将应用于user表,而不是JOIN

我认为这应该适合你:

$criteria = new CDbCriteria();
$criteria->alias = "u";
$criteria->together= "skill_assessment";
$where = array();

for($i = 0; $i < count($skill_filters); $i++) {
    $where[] = "(s.skill='".$skill_filters[$i]->skill."' AND s.score >= ".$skill_filters[$i]->level . ')';
}
$criteria->join = 'JOIN skill_assessment s ON (u.id=s.userId' . ( $where ? ( ' AND (' . join( ' OR ', $where ) . ')' ) : '') . ')';

$users = UserModel::model()->findAll($criteria);

像这样获取_skill_assessment_:

foreach( $users->skill_assessment as $skill_assessment )
{
    echo $skill_assessment->userId;
}
于 2012-06-20T09:07:02.483 回答
1

在这个级别保持简单:

$sql="select * from mytable where id = :id";
$cmd = Yii::app()->db->createCommand($sql)->bindParam("id", $res_id);
$cmd->execute();
于 2012-06-20T06:22:34.080 回答
0

非常感谢鲍里斯·贝伦斯基!你的建议不是所需要的,但它确实让我得到了我所需要的。这是我最终得到的结果:

$str = "abcdefghijklmnopqrstuvwxyz";
            $idxs = str_split($str);

            $criteria = new CDbCriteria();
            $criteria->alias = "u";
            $joins = array();
            for($i = 0; $i < count($skill_filters); $i++)
            {
                $joins[] = " JOIN (SELECT * FROM skill_assessment s WHERE s.skill = '" . $skill_filters[$i]->skill ."' AND s.score >= " . $skill_filters[$i]->level . ") " . $idxs[$i] . " ON (u.id = " . $idxs[$i] . ".userId) ";
            }

            $criteria->join = join(' ', $joins);
            $users = UserModel::model()->findAll($criteria);
            foreach($users as $user)
            {
                $user->skill_assessments = $user->getRelated('skill_assessments');
            }

真正关键的部分是 $joins 数组。这将允许您获取与 Skill_assessments 表中的所有行匹配条件匹配的所有用户。

此外,我可能会在普通视图中懒惰地加载视图中的技能评估,但我现在需要获取每个用户的所有技能评估,以便在与用户的 ajax 响应中将它们传回。

于 2012-06-21T02:45:11.707 回答