CRUD $model->save() not working

Hi All,

I’m running Yii on OSX Mountain Lion and have generated CRUD using Gii. It created fine once I changed permissions, but will not allow create or updates to save to the database. After clicking the save or update it just flips to the home url. Logs look fine and not errors.

I found a similar error out here to do with lowercase models, but that is not the situation in this case.

Any help would be appreciated.

Thanks,

Remo

Hello

Can you share your code?

And… what are you trying to update if you haven’t been able to save?

Cheers

thanks for your reply. There really isn’t any custom code. I have created CRUD section using CRUD generator in Gii. The output of the CRUD generator does not fully work. I does not allow me to use the ‘create’ and ‘update’ functions, in that when I press ‘create’ or ‘update’, it seems to just flip to the home page.

Remo

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,

		));

	}

}

It’'s resolved. I had another sub form with a submit button in my layout and it was posting to that. I should of caught it sooner.

Thanks for your help.

Remo