-4

我将如何在 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;
    }
}
4

5 回答 5

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();
于 2012-06-08T07:32:51.460 回答
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;
        }
    }
于 2012-06-08T11:56:08.450 回答
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);    
于 2015-07-22T10:31:42.317 回答
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)); 
于 2017-04-20T04:31:17.843 回答
-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);
于 2016-06-01T06:39:51.503 回答