加入YII框架

时间:2012-06-08 06:44:36

标签: yii

我将如何在yii框架中编写以下查询

function Get_Services($user_id, $isResultSet = true){
    $sql = $this->select()
                ->setIntegrityCheck(false)
                    ->from(array('p' => 'phone_service'))
                    ->join(array('u' => 'user_preferences'), 'u.phone_service_id = p.phone_service_id')
                    ->where('u.user_preferences_name = ?', 'is_user_package_active')
                    ->where('p.user_id = ?', $user_id);

    if($isResultSet){
        return $sql->query()->fetchAll();
    }else{
        return $sql;
    }
}

5 个答案:

答案 0 :(得分:2)

大致是:

$sql = Yii::app()->db->createCommand()
   ->from('phone_service p')
   ->join('user_preferences u', 'u.phone_service_id = p.phone_service_id')
   ->where('u.user_preferences_name = :upn', array(':upn' => 'is_user_package_active'))
   ->where('p.user_id = :uid', array(':uid' => $user_id)
   ->queryAll();

答案 1 :(得分:0)

试试这个......

function Get_Services($user_id, $isResultSet = true){
        $sql = $this->select()
                    ->setIntegrityCheck(false)
                        ->from(array('p' => 'phone_service'))
                        ->join(array('u' => 'user_preferences'), 'u.phone_service_id = p.phone_service_id')
                        ->where('u.user_preferences_name = ?', 'is_user_package_active')
                        ->where('p.user_id = ?', $user_id);

        if($isResultSet){
            return $sql->queryAll();
        }else{
            return $sql;
        }
    }

答案 2 :(得分:0)

$sql = Yii::app()->db->createCommand()
->from('phone_service p')
->join('user_preferences u', 'u.phone_service_id = p.phone_service_id')
->where('u.user_preferences_name=:preferences',array(':preferences'=>'is_user_package_active'))
->where('p.user_id = :uid', array(':uid' => $user_id)
->queryAll(true);    

答案 3 :(得分:0)

我建议您为此创建模型,使用Yii深入了解关系,并使用有效记录和with()调用进行联接。

之后可能是一个简单的电话
$records = PhoneServices::model()->with(array('userPreferences' => 
 array('condition' => 'userPreferences.user_preferences_name = 
 "is_user_package_active" ')))->findAllByAttributes(array('user_id' => $user_id)); 

答案 4 :(得分:-1)

尝试以下

$sql = $this->select()
                ->setIntegrityCheck(false)
                    ->from(array('p' => 'phone_service'))
                    ->join(array('u' => 'user_preferences'), 'u.phone_service_id = p.phone_service_id')
                    ->where('u.user_preferences_name = ?', 'is_user_package_active')
                    ->where('p.user_id = ?', $user_id);
$query= $model->findAll($sql);