Property xxx is not defined.(SOLVED)

in MedicController.php




<?php

class MedicController extends Controller

{

	/**

	 * Creates a new model.

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

	 */

	public function actionCreate()

	{

			

		$form = new CForm('application.views.medic.createForm');

		$form['user1']->model = new User1();

		$form['post1']->model = new Post1();

		$form['post2']->model = new Post2();

		if($form->submitted('register'))

		{

			$user1 = $form['user1']->model;

			$post1 = $form['post1']->model;

			$post2 = $form['post2']->model;

			

			if($user1->save(false))

			{

				$post1->user_id = $user1->id;

				$post1->save(false);

				$post2->user_id = $user1->id;

				$post2->save(false);

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

			}

		}

		$this->render('create', array('form'=>$form));

	}

}



In Post1.php




<?php


/**

 * This is the model class for table "tbl_post1".

 *

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

 * @property integer $id

 * @property integer $attribute1

 * @property integer $attribute2

 * @property integer $user_id

 *

 * The followings are the available model relations:

 * @property User1 $user

 */

class Post1 extends CActiveRecord

{

	/**

	 * Returns the static model of the specified AR class.

	 * @param string $className active record class name.

	 * @return Post1 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 'tbl_post1';

	}


	/**

	 * @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('user_id', 'required'),

			array('attribute1, attribute2, user_id', 'numerical', 'integerOnly'=>true),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('id, attribute1, attribute2, user_id', '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(

			'user' => array(self::BELONGS_TO, 'User1', 'user_id'),

		);

	}


	/**

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

	 */

	public function attributeLabels()

	{

		return array(

			'id' => 'ID',

			'attribute1' => 'Attribute1',

			'attribute2' => 'Attribute2',

			'user_id' => 'User',

		);

	}


	/**

	 * 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('attribute1',$this->attribute1);

		$criteria->compare('attribute2',$this->attribute2);

		$criteria->compare('user_id',$this->user_id);


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

}



in User1.php




<?php


/**

 * This is the model class for table "tbl_user1".

 *

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

 * @property integer $id

 * @property string $username

 *

 * The followings are the available model relations:

 * @property Post1[] $post1s

 * @property Post2[] $post2s

 */

class User1 extends CActiveRecord

{

	/**

	 * Returns the static model of the specified AR class.

	 * @param string $className active record class name.

	 * @return User1 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 'tbl_user1';

	}


	/**

	 * @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('username', 'required'),

			array('username', 'length', 'max'=>128),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('id, username', '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(

			'post1s' => array(self::HAS_MANY, 'Post1', 'user_id'),

			'post2s' => array(self::HAS_MANY, 'Post2', 'user_id'),

		);

	}


	/**

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

	 */

	public function attributeLabels()

	{

		return array(

			'id' => 'ID',

			'username' => 'Username',

		);

	}


	/**

	 * 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('username',$this->username,true);


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

	

	public function getGenderOptions()

	{

		return array(

				0 => 'Male',

				1 => 'Female',

		);

	}

	

}



In createForm.php




<?php

return array(

		'elements'=>array(

				'user1'=>array(

						'type'=>'form',

						'title'=>'User Record',

						'elements'=>array(

								'username'=>array(

										'type'=>'text',

								),

						),

				),

				'post1'=>array(

						'type'=>'form',

						'title'=>'Post1',

						'elements'=>array(

								'attribute1'=>array(

										'type'=>'dropdownlist',

										'items'=>User1::model()->getGenderOptions(),

										'prompt'=>'Please select:',

										

								),

								'attribute2'=>array(

										'type'=>'dropdownlist',

										'items'=>User1::model()->getGenderOptions(),

										'prompt'=>'Please select:',

										

								),

						),

				),

				'post2'=>array(

						'type'=>'form',

						'title'=>'Post2',

						'elements'=>array(

								'attribute3'=>array(

										'type'=>'dropdownlist',

										'items'=>User1::model()->getGenderOptions(),

										'prompt'=>'Please select:',

				

								),

								'attribute4'=>array(

										'type'=>'dropdownlist',

										'items'=>User1::model()->getGenderOptions(),

										'prompt'=>'Please select:',

				

								),

						),

				),

		),

		'buttons'=>array(

				'register'=>array(

						'type'=>'submit',

						'label'=>'Register',

				),

		),

);




In create.php




<h1>Register</h1>

 

<div class="form">

<?php echo $form; ?>

</div>



after hit the submit button I got

Property "Post1.user_id" is not defined. Is this a bug?

Somebody please help me.

I found it ,there is a typos in my table (column name = $user_id).

My bad.

Thanks everyone.

SOLVED

Hi. Just a guess :)

In your controller, after saving your new user, you have to refresh the instance to get its id, like this :




if($user1->save(false))

{

    $user1->refresh();

    $post1->user_id = $user1->id;

    $post1->save(false);

    $post2->user_id = $user1->id;

    $post2->save(false); 

    ...



Thanks for your quick reply,but it doesn’ t work.($user1->id did generate its id)