如何在 yii 中使用 findAll() 编写查询 [英] How to write the query in yii,using findAll()

查看:29
本文介绍了如何在 yii 中使用 findAll() 编写查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 sql 查询,如何在 yii 中使用 findAll() 编写查询?

I have the following sql query, how can I write the query in yii using findAll()?

我没有使用 CDbCriteria 并且暂时避免使用它.

I am not using CDbCriteria and for the time being avoiding it.

使用三个表 user、category 和 job_profile.

Using three tables user, category and job_profile.

SELECT * 
FROM job_profile
INNER JOIN user ON user.id = job_profile.user_id
INNER JOIN category ON category.id = job_profile.category_id
WHERE category_id = 1
  AND experience = 2
  AND key_skills LIKE  '%php%'

我编写了以下查询,但我不知道如何包含连接:

I have written the following queries but I do not know how to include join:

 $results = SearchEmployee::model()->findAll("category_id=:category AND key_skills like 
:skill AND experience=:experience", array(
            'category'=>$category,
            'skill'=>'%'.$skills.'%',
            'experience'=>$experience
            ));        

推荐答案

$model = JobProfile::model()->with('userrelationname','categoryrelationname')->findAll(array("condition"=>"t.category_id =1 and t.experience =2 and t.key_skills LIKE '%php%'"));

您可以在 JobProfile 模型中找到关系名称

You can find relations names in your JobProfile model

关系示例

您的 JobProfile 模型文件中会有源代码,例如

There will be source code in your JobProfile Model File like

 public function relations(){
    return array(
      'user' => array (self::BELONGS_TO, 'User', 'id' ),
    )
 }

这里的用户"是关系名称...

Here is 'user' is relation name...

然后在您的控制器示例中;

then in your controller example;

foreach($model as $m){
    echo $m['user_id']; // in your job_profile table
    echo $m->user->id;  // in your user table
}

这篇关于如何在 yii 中使用 findAll() 编写查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆