Sorry to ask but I'm confused. Record not saving...

This has me freaking out but probably a stupid oversight on my part.

I have a problem with a single model that just doesn’t want to save.

The crud and model were generated with yiic and slightly modified, but it insists the required data is blank.

Take a look at the firebug screenshot where you can see the post values are indeed being sent, yet Yii reckons they’re not.

Here’s the controller, model and view codes, with the firebug screenshot attached (we’re looking at the ‘create’ action here):

Conroller:


<?php


class ClientsController extends CController

{

	const PAGE_SIZE=10;


	/**

	 * @var CActiveRecord the currently loaded data model instance.

	 */

	private $_model;


	/**

	 * @return array action filters

	 */

	public function filters()

	{

		return array(

			'accessControl', // perform access control for CRUD operations

		);

	}


	/**

	 * Specifies the access control rules.

	 * This method is used by the 'accessControl' filter.

	 * @return array access control rules

	 */

	public function accessRules()

	{

		return array(

			array('allow',  // allow all users to perform 'index' and 'view' actions

				'actions'=>array('index','view'),

				'users'=>array('*'),

			),

			array('allow', // allow authenticated user to perform 'create' and 'update' actions

				'actions'=>array('create','update'),

				'users'=>array('@'),

			),

			array('allow', // allow admin user to perform 'admin' and 'delete' actions

				'actions'=>array('admin','delete'),

				'users'=>array('admin'),

			),

			array('deny',  // deny all users

				'users'=>array('*'),

			),

		);

	}


	/**

	 * Displays a particular model.

	 */

	public function actionView()

	{

		$this->render('view',array(

			'model'=>$this->loadModel(),

		));

	}


	/**

	 * Creates a new model.

	 * If creation is successful, the browser will be redirected to the 'view' page.

	 */

	public function actionCreate()

	{

		$model=new clients;

		if(isset($_POST['clients']))

		{

			$model->attributes=$_POST['clients'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->id));

		}


                $model->created = date('Y-m-d');

		$this->render('create',array(

			'model'=>$model,

		));

	}


	/**

	 * Updates a particular model.

	 * If update is successful, the browser will be redirected to the 'view' page.

	 */

	public function actionUpdate()

	{

		$model=$this->loadModel();

		if(isset($_POST['clients']))

		{

			$model->attributes=$_POST['clients'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->id));

		}


		$this->render('update',array(

			'model'=>$model,

		));

	}


	/**

	 * Deletes a particular model.

	 * If deletion is successful, the browser will be redirected to the 'index' page.

	 */

	public function actionDelete()

	{

		if(Yii::app()->request->isPostRequest)

		{

			// we only allow deletion via POST request

			$this->loadModel()->delete();


			// if AJAX request (triggered by deletion via admin grid view)

			// we should not redirect the browser

			if(!Yii::app()->request->isAjaxRequest)

				$this->redirect(array('index'));

		}

		else

			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');

	}


	/**

	 * Lists all models.

	 */

	public function actionIndex()

	{

		$dataProvider=new CActiveDataProvider('clients', array(

			'pagination'=>array(

				'pageSize'=>self::PAGE_SIZE,

			),

		));


		$this->render('index',array(

			'dataProvider'=>$dataProvider,

		));

	}


	/**

	 * Manages all models.

	 */

	public function actionAdmin()

	{

		$dataProvider=new CActiveDataProvider('clients', array(

			'pagination'=>array(

				'pageSize'=>self::PAGE_SIZE,

			),

		));


		$this->render('admin',array(

			'dataProvider'=>$dataProvider,

		));

	}


	/**

	 * Returns the data model based on the primary key given in the GET variable.

	 * If the data model is not found, an HTTP exception will be raised.

	 */

	public function loadModel()

	{

		if($this->_model===null)

		{

			if(isset($_GET['id']))

				$this->_model=clients::model()->findbyPk($_GET['id']);

			if($this->_model===null)

				throw new CHttpException(404,'The requested page does not exist.');

		}

		return $this->_model;

	}

}

Model:


<?php


class clients extends CActiveRecord

{

	/**

	 * The followings are the available columns in table 'clients':

	 * @var integer $id

	 * @var integer $types_id

	 * @var integer $statuses_id

	 * @var string $name

	 * @var string $telephone

	 * @var string $fax

	 * @var string $website

	 * @var string $email

	 * @var string $created

	 * @var integer $enabled

	 */


	/**

	 * Returns the static model of the specified AR class.

	 * @return CActiveRecord 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 'clients';

	}


	/**

	 * @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('types_id, statuses_id, name', 'required'),

			array('types_id, statuses_id, enabled', 'numerical', 'integerOnly'=>true),

			array('name, website', 'length', 'max'=>255),

			array('telephone, fax', 'length', 'max'=>45),

			array('email', 'length', 'max'=>100),

			array('created', 'safe'),

		);

	}


	/**

	 * @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(

			'addresses' => array(self::HAS_MANY, 'Addresses', 'clients_id'),

			'attributes' => array(self::HAS_MANY, 'Attributes', 'clients_id'),

			'statuses' => array(self::BELONGS_TO, 'Statuses', 'statuses_id'),

			'types' => array(self::BELONGS_TO, 'Types', 'types_id'),

			'users' => array(self::MANY_MANY, 'Users', 'clients_has_users(clients_id, users_id)'),

			'contracts' => array(self::HAS_MANY, 'Contracts', 'clients_id'),

			'notes' => array(self::HAS_MANY, 'Notes', 'clients_id'),

			'resources' => array(self::HAS_MANY, 'Resources', 'clients_id'),

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'id' => 'Id',

			'types_id' => 'Type',

			'statuses_id' => 'Status',

			'name' => 'Name',

			'telephone' => 'Telephone',

			'fax' => 'Fax',

			'website' => 'Website',

			'email' => 'Email',

			'created' => 'Created',

			'enabled' => 'Enabled',

		);

	}

}

View:


<div class="form">


<?php echo CHtml::beginForm(); ?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>


	<?php echo CHtml::errorSummary($model); ?>


	<div class="row">

		<?php echo CHtml::activeLabelEx($model,'types_id'); ?>

		<?php echo CHtml::activeDropDownList($model,'types_id',CHtml::listData( types::model()->findAll(), 'id', 'name' ), array('prompt'=>'Select Type')); ?>

		<?php echo CHtml::error($model,'types_id'); ?>

	</div>


	<div class="row">

		<?php echo CHtml::activeLabelEx($model,'statuses_id'); ?>

		<?php echo CHtml::activeDropDownList($model,'statuses_id',CHtml::listData( statuses::model()->findAll(), 'id', 'name' ), array('prompt'=>'Select Status')); ?>

		<?php echo CHtml::error($model,'statuses_id'); ?>

	</div>


	<div class="row">

		<?php echo CHtml::activeLabelEx($model,'name'); ?>

		<?php echo CHtml::activeTextField($model,'name',array('size'=>60,'maxlength'=>255)); ?>

		<?php echo CHtml::error($model,'name'); ?>

	</div>


	<div class="row">

		<?php echo CHtml::activeLabelEx($model,'telephone'); ?>

		<?php echo CHtml::activeTextField($model,'telephone',array('size'=>45,'maxlength'=>45)); ?>

		<?php echo CHtml::error($model,'telephone'); ?>

	</div>


	<div class="row">

		<?php echo CHtml::activeLabelEx($model,'fax'); ?>

		<?php echo CHtml::activeTextField($model,'fax',array('size'=>45,'maxlength'=>45)); ?>

		<?php echo CHtml::error($model,'fax'); ?>

	</div>


	<div class="row">

		<?php echo CHtml::activeLabelEx($model,'website'); ?>

		<?php echo CHtml::activeTextField($model,'website',array('size'=>60,'maxlength'=>255)); ?>

		<?php echo CHtml::error($model,'website'); ?>

	</div>


	<div class="row">

		<?php echo CHtml::activeLabelEx($model,'email'); ?>

		<?php echo CHtml::activeTextField($model,'email',array('size'=>60,'maxlength'=>100)); ?>

		<?php echo CHtml::error($model,'email'); ?>

	</div>


	<div class="row">

		<?php echo CHtml::activeLabelEx($model,'created'); ?>

		<?php echo CHtml::activeTextField($model,'created', array('readonly'=>true)); ?>

		<?php echo CHtml::error($model,'created'); ?>

	</div>


	<div class="row">

		<?php echo CHtml::activeLabelEx($model,'enabled'); ?>

		<?php echo CHtml::activeCheckBox($model,'enabled'); ?>

		<?php echo CHtml::error($model,'enabled'); ?>

	</div>


	<div class="row buttons">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>

	</div>


<?php echo CHtml::endForm(); ?>


</div><!-- form -->

I’m using the latest 1.1RC

Any help would be greatly appreciated :)426

firebug.jpg

OK we can put this one to bed.

The problem is that I have a table named ‘attributes’ and the relations() in the model are interfering with the models own ‘attributes’ array which should be filled with the post values.

I’ve commented out the offending line in the code below so you can see what I mean:


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(

			'addresses' => array(self::HAS_MANY, 'Addresses', 'clients_id'),

			//'attributes' => array(self::HAS_MANY, 'Attributes', 'clients_id'),

			'statuses' => array(self::BELONGS_TO, 'Statuses', 'statuses_id'),

			'types' => array(self::BELONGS_TO, 'Types', 'types_id'),

			'users' => array(self::MANY_MANY, 'Users', 'clients_has_users(clients_id, users_id)'),

			'contracts' => array(self::HAS_MANY, 'Contracts', 'clients_id'),

			'notes' => array(self::HAS_MANY, 'Notes', 'clients_id'),

			'resources' => array(self::HAS_MANY, 'Resources', 'clients_id'),

		);

	}

So simple yet took me so long to figure out lol.