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

これはルールメソッドです。他のモデルやコントローラーでは同じことがうまく機能しているのに、なぜこれが機能しないのかわかりません。

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

どうやらあなたの場合(エラーの原因)、両方の条件チェックがfalseであるため、rules-method は何も返しません。

メソッドに追加return array();してみてくださいrules。両方のチェックが失敗した場合の一種のフォールバックになります。

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