0

我希望能够使用 Yii 框架读取多行,而不必创建多个模型对象。考虑到这一点,我正在设置一个 CDbCriteria,其条件为 RowName = :param0 OR RowName = param1... RowName = ParamN 和相应的 params 值。

我的问题有两个。首先是 findall 失败。第二个是我不知道如何从 findall 调用中获取错误代码(因此返回“heartBeat”行)。

class Xactions extends CActiveRecord
{
.
.
.
//example $requests data: '192.168.1.162, 192.168.1.161'
//This function should return a Xactions model which is associated with any rows
// where tcpAddress = $requests Comma separated values.
public static function HeartBeatmodel($requests)
{
        $criteria=new CDbCriteria;
        $request_arr =  explode(',', $requests);
        $criteria->condition=Xactions::request_condition("tcpAddress", count($request_arr));;
        $criteria->params= Xactions::request_params($request_arr);
        $return= Xactions::model()->findall($criteria);
        return "heartBeat"; // here for debugging purposes
}
public static function request_condition($column_name, $num_request){
        //want to end up with name=:param1 OR name=:param2....
        $return_str = "";
        for ($i = 0; $i < $num_request; $i++){
            $return_str .= $column_name . '=param' . (string)$i . ' ';
            if($i == $num_request -1 ) break; //prevents extra OR statement
            $return_str .= 'OR ';
        }
        return $return_str;
}

public static function request_params($requests){
        $return_arr = array();
        for ($i = 0; $i < count($requests); $i++){
            $return_arr['param' . (string)$i] = $requests[$i];
        }
        return $return_arr;
    }
4

1 回答 1

1

也许这样的事情可以工作?

class Xactions extends CActiveRecord {
        public static function HeartBeatmodel($requests) {
                $criteria = new CDbCriteria;
                $request_arr =  explode(',', $requests);

                for($request_arr as $request) {
                    $criteria->compare('tcpAddress', $request, false, 'OR');
                }

                return Xactions::model()->findAll($criteria);
        }

我正在使用 CDbCriteria->compare,您可以在此处找到规范:http ://www.yiiframework.com/doc/api/1.1/CDbCriteria#compare-detail

正如你所看到的,我设置$partialMatch为 false 和$operatortoOR所以它应该完成你想要的,对吧?

编辑:我刚刚意识到,最聪明的方法就是这样

public static function HeartBeatmodel($requests) {
  $criteria = new CDbCriteria;
  $criteria->addInCondition('tcpAddress', explode(',', $requests));

  return Xactions::model()->findAll($criteria);
}
于 2013-04-25T19:42:54.063 回答