Below is the standard code generated by GII for CRUD. I tried printing errors or sending to another page if $model->save() is false, but it keeps flipping to home url. It’s like it’s crashing and defaulting back to home page.
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Admin']))
{
$model->attributes=$_POST['Admin'];
if($model->save()){
//var_dump($model->getErrors());
$this->redirect(array('view','id'=>$model->id));}
}
$this->render('update',array(
'model'=>$model,
));
Here is the view:
<?php
$this->breadcrumbs=array(
'Admins'=>array('index'),
$model->title=>array('view','id'=>$model->id),
'Update',
);
$this->menu=array(
array('label'=>'List Admin', 'url'=>array('index')),
array('label'=>'Create Admin', 'url'=>array('create')),
array('label'=>'View Admin', 'url'=>array('view', 'id'=>$model->id)),
array('label'=>'Manage Admin', 'url'=>array('admin')),
);
?>
<h1>Update Admin <?php echo $model->id; ?></h1>
<?php echo $this->renderPartial('_form', array('model'=>$model)); ?>
Here is the model:
<?php
/**
* This is the model class for table "admin".
*
* The followings are the available columns in table 'admin':
* @property integer $id
* @property string $title
* @property string $url
* @property string $description
*/
class Admin extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return Admin the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'admin';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('title', 'length', 'max'=>20),
array('url', 'length', 'max'=>45),
array('description', 'length', 'max'=>256),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, title, url, description', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'title' => 'Title',
'url' => 'Url',
'description' => 'Description',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('title',$this->title,true);
$criteria->compare('url',$this->url,true);
$criteria->compare('description',$this->description,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}