duanqiang7631 2013-04-25 18:27
浏览 17
已采纳

使用Yii活动记录从DB读取多行

I would like to be able to read multiple rows using the Yii framework without having to create multiple model objects. With that In mind, I'm setting up a CDbCriteria with a condition of the form RowName = :param0 OR RowName = param1... RowName = ParamN and a corresponding params value.

My problems are two fold. The first is that the findall fails. The second is that I don't know how to get an error code out of the findall call (thus the return "heartBeat" line).

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;
    }
  • 写回答

1条回答 默认 最新

  • doutang3760 2013-04-25 19:42
    关注

    Maybe something like this could work?

    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);
            }
    

    I'm using CDbCriteria->compare, you can find the specification here: http://www.yiiframework.com/doc/api/1.1/CDbCriteria#compare-detail

    As you can see, I set $partialMatch to false and the $operator to OR so it should accomplish what you want, right?

    edit: I just realized, the smartest way to do this would be like this

    public static function HeartBeatmodel($requests) {
      $criteria = new CDbCriteria;
      $criteria->addInCondition('tcpAddress', explode(',', $requests));
    
      return Xactions::model()->findAll($criteria);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Java-Oj-桌布的计算
  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路