-2

这是我的代码,当我调用此方法时发生错误并说“为 foreach() 提供的参数无效”。任何人?任何想法 ?

$state = array(
          'state_status_id' => '4', 
          'fax_format_preference_id' => '2', 
          'state_reference_code' => $state_ref->reference_code, 
          'leap_fqdn' => $leap_fqdn ,
          'description' => $state_ref->name."--".$leap_fqdn,
          );
  $model = new States;
  $model->setAttributes($state, false);
  $model->setIsNewRecord(true);

  $res = $model->save();

这是堆栈信息:

   public function createValidators(){
       $validators=new CList;
       foreach($this->rules() as $rule)
       {
           if(isset($rule[0],$rule[1]))  // attributes, validator name
             $validators->add(CValidator::createValidator($rule[1],$this,$rule[0],array_slice($rule,2)));
           else
             throw new CException(Yii::t('yii','{class} has an invalid validation rule. The rule must specify attributes to be validated and the validator name.',
                 array('{class}'=>get_class($this))));
        }
       return $validators;
     }

这是 Rule 方法,我不知道为什么这不起作用,而其他模型和控制器中同样的东西工作正常。

public function rules()
{   
    //Rules for admins editing state information
    if(Roles::has_permission('update','all_states'))
    {
        return array(
            array('state_status_id, fax_format_preference_id, state_reference_code, leap_fqdn, description', 'required'),
            array('state_status_id, fax_format_preference_id', 'length', 'max'=>10),
            array('leap_fqdn', 'length', 'max'=>128),
            array('description', 'length', 'max'=>255),
            array('updated_at,default_user_role', 'safe'),
            // Please remove those attributes that should not be searched.
            array(' state_status_id, fax_format_preference_id, created_at, updated_at, state_reference_code, leap_fqdn, description', 'safe', 'on'=>'search'),
        );
    }

    if(Roles::has_permission('update','state_state'))
    {
        return array(
            array('fax_format_preference_id', 'required'),
            array('fax_format_preference_id', 'length', 'max'=>10),
            array('updated_at', 'safe'),
        );
    }
}
4

1 回答 1

1

显然,在您的情况下(导致错误),两个条件检查都是错误的,因此rules-method 什么也不返回。

尝试添加return array();到该rules方法中,以防两个检查都失败,这将是一种后备。

public function rules()
{
    // your current code
    return array(); //fallback
}
于 2013-09-19T20:13:35.670 回答