不幸的是,这个解决方案无法保存在网格上编辑的数据,我可以提供一些帮助吗?
提前致谢。
查看
<form name="editableGridForm" method="post" action="editableGrid"> <?php
$this->widget('zii.widgets.grid.CGridView',
array(
'id'=>'timesheet-grid',
'dataProvider'=>$model->search(),
'columns'=>array(
array('name'=>'ID','htmlOptions'=>array('width'=>'25px')),
array('name'=>"DESCRIZIONE",
'type'=>'raw',
'htmlOptions'=>array('width'=>'150px'),
'value'=>'$data->getInputField(\'DESCRIZIONE\',$row)'
),
)
)
);
echo CHtml::submitButton('salva',array('class' => 'btn btn-success'));
?>
</form>
型号
public function getInputField($fieldName, $row, $options = array()) {
return CActiveForm::textField($this, $fieldName,
array_merge(array("name" => "Timesheet[" . $row . "][" . $fieldName . "]"), $options)
);
}
控制器
public function actionEditableGrid() {
$model=new Timesheet('search');
$notenData = $model->search();
if (isset($_POST['Timesheet']) ) {
foreach( $notenData->data as $i=>$item ) {
if(isset($_POST['Timesheet'][$i])) {
$item->attributes=$_POST['Timesheet'][$i];
if ( $item->validate() ) {
$item->save();
}
}
}
$this->redirect(array('admin'));
}
}
当你对
$this->attributes
进行批量赋值时,Yii 只会更改“安全”属性。当属性具有验证规则或仅被标记为安全时(如果没有合适的验证规则),属性就是安全的,如下所示:
public function rules()
{
return array(
array('DESCRIZIONE', 'required'),
array('DESCRIZIONE', 'safe'),
);
}
这些规则之一就足够了。
有关进行批量分配和“安全”验证器的更多信息:http://www.yiiframework.com/wiki/161/understanding-safe-validation-rules/
我还注意到,您应该在模型中将
CActiveForm::textField
替换为 CHtml::activeTextField
,以防止 php 严格模式下出现错误。 textField
是 CActiveForm
的非静态方法。
编辑:为了调试保存不起作用的原因,请在控制器中替换它
if ( $item->validate() ) {
$item->save();
}
由
if ( !$item->save() ) {
var_dump('Could not save, errors: ' . var_export($item->errors, TRUE) . ', attributes: ' . var_export($item->attributes, TRUE));
return;
}
save()
会自动调用validate()
,如果验证失败则返回false
。