0

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;
    }
2
  • it's spelled findAll? maybe that's the problem? Commented Apr 25, 2013 at 19:36
  • No, that wasn't the problem sadly. Commented Apr 26, 2013 at 7:09

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

This looks promising, I'll have to test this tomorrow and report back my findings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.