Activerecord - peroperty is not defined

hello,

i have 2 model (activerecord), customer.php & address.php.

and than i want to create record use activerecord.

here my code:




private function saveCustomer($model){

		// save customer		

		$cust = new Customer();

		try{

			

			// save customer address

			$add = new Address();

			$add->company = $model->company;

			$add->firstname = $model->firstname;

			$add->lastname = $model->lastname;



i get error:




Property "Address.company" is not defined.



and this address model




<?php


/**

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

 *

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

 * @property integer $address_id

 * @property integer $customer_id

 * @property string $company

 * @property string $firstname

 * @property string $lastname

 * @property string $address_1

 * @property string $address_2

 * @property string $postcode

 * @property string $city

 * @property integer $country_id

 * @property integer $zone_id

 */

class Address extends CActiveRecord

{

	/**

	 * Returns the static model of the specified AR class.

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

	}


	/**

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

			array('customer_id, country_id, zone_id', 'numerical', 'integerOnly'=>true),

			array('company, firstname, lastname', 'length', 'max'=>32),

			array('address_1, address_2, city', 'length', 'max'=>128),

			array('postcode', 'length', 'max'=>10),

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

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

			array('address_id, customer_id, company, firstname, lastname, address_1, address_2, postcode, city, country_id, zone_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(

		);

	}


	/**

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

	 */

	public function attributeLabels()

	{

		return array(

			'address_id' => 'Address',

			'customer_id' => 'Customer',

			'company' => 'Company',

			'firstname' => 'Firstname',

			'lastname' => 'Lastname',

			'address_1' => 'Address 1',

			'address_2' => 'Address 2',

			'postcode' => 'Postcode',

			'city' => 'City',

			'country_id' => 'Country',

			'zone_id' => 'Zone',

		);

	}


	/**

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

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

		$criteria->compare('company',$this->company,true);

		$criteria->compare('firstname',$this->firstname,true);

		$criteria->compare('lastname',$this->lastname,true);

		$criteria->compare('address_1',$this->address_1,true);

		$criteria->compare('address_2',$this->address_2,true);

		$criteria->compare('postcode',$this->postcode,true);

		$criteria->compare('city',$this->city,true);

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

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


		return new CActiveDataProvider(get_class($this), array(

			'criteria'=>$criteria,

		));

	}

}



thanks

yes , that can’t find company field in Address model!!!

It seems that the code was generated by gii. it should work, cauze gii wrote this


* @property string $company

.

Maybe you have defined two Adress class in your project then maybe yii already loaded one of them which has not company attr :(

I agree with elbek, and in addition I wonder what the parameter $model stands for.

I also have examples of exactly this kind with different tables, and the way that I use to run well. This makes me confused.

then what’s the solution?

This also occurs with the Customer, with the same error.

this compaltate code:




public function actionRegister (){

		$model = new CustomerForm('register');


		// collect user input data

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

		{

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

			// validate user input and redirect to the previous page if valid

			if($model->validate()){

			

				$this->saveCustomer($model);

				

			}

		}

		

		.....................

	}



I have found the solution that the class name should not be the same as the name of the table.




class TAddress extends CActiveRecord

{

        /**

         * Returns the static model of the specified AR class.

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

        }






$add = new TAddress();



is there any article that can explain this?

Most probably not true. Works for me.

/Tommy

of conditions that I experienced can you explain why could happen like this? What is wrong with the code that I created

thank you