Php 为什么我能';你不能用Yii更新这个模型吗?

Php 为什么我能';你不能用Yii更新这个模型吗?,php,validation,yii,model,Php,Validation,Yii,Model,我正在使用Yii框架,我想通过单击在控制器中调用操作的链接来更新模型,此操作用于修改模型的某些属性,如果修改完成,则应重定向到另一个操作。我的问题是模型更新没有保存,我不知道为什么,这是我的代码: 查看代码: <table class="table table-striped"> <thead> <tr> <th>#</th> <th>title<

我正在使用Yii框架,我想通过单击在控制器中调用操作的链接来更新模型,此操作用于修改模型的某些属性,如果修改完成,则应重定向到另一个操作。我的问题是模型更新没有保存,我不知道为什么,这是我的代码:

查看代码:

<table class="table table-striped">
    <thead>
        <tr>
            <th>#</th>
            <th>title</th>
            <th>category</th>
            <th>published</th>
            <th>priority</th>
            <th>in news banner</th>
            <th>publish</th>
        </tr>
    </thead>
    <tbody>
<?php
$x=0;
if(!empty($model)){

    foreach ($model as $m){
        $x++;
        echo "<tr>
            <td>$x</td>
            <td>$m->title</td>
                <td>$m->category</td>
                    <td>$m->display</td>
                        <td>$m->priority</td>
                            <td>$m->newsBanner</td>
                        <td><a href='".Yii::app()->createUrl('articles/approve', array('id'=>$m->id, 'c'=>$c))."'><button id='btn$m->id'>Publish</button></a></td>
                </tr>"; 
    }
}
?>
    </tbody></table>
模型规则代码:

public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('text, category, display, newsBanner, priority, title, visits, previewText, idUser, homepage', 'required'),
            array('idUser, homepage', 'numerical', 'integerOnly'=>true),
            array('visits', 'numerical'),
            array('category, priority', 'length', 'max'=>50),
            array('display, newsBanner', 'length', 'max'=>3),
            array('date', 'safe'),
            array('image', 'file', 'types'=>'jpg, jpeg, JPG, png, PNG, gif, GIF', 'allowEmpty'=>true, 'on'=>'update'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, text, category, display, newsBanner, priority, date, title, visits, previewText, image, caption, idUser, homepage', 'safe', 'on'=>'search'),
        );
    }
我从
getErrors()
中得到了空数组,那么问题出在哪里?

validate()
方法在调用后清除错误(作为默认行为)。如果您查看官方文件,您将看到:

public boolean validate(array $attributes=NULL, boolean $clearErrors=true)
$attributes:应验证的属性列表。默认值为null,这意味着应验证适用验证规则中列出的任何属性。如果此参数作为属性列表提供,则仅验证列出的属性

$clearErrors:在执行验证之前是否调用clearErrors

$clearErrors
默认设置为
TRUE
。因此,在调用
validation()
方法之后,您的
getErrors()
方法将不会出错

尝试:

或:


@MD.MD您还有一个打字错误:
$artilce->save()
应该是
$article->save()
该死!!就是这样,只是拼错了mistake@MD.MD很高兴它起作用了。但是,调用
$model->validate()
后,
$model->getErrors()
将始终为空
public boolean validate(array $attributes=NULL, boolean $clearErrors=true)
if($article->validate()){
    //then save 
}
$article->validate(NULL,FALSE);
//check errors