Property classname.relationName is not defined

Hello,

I am only trying to get a single record from 2 database tables that are related. I am getting property classname.relationname in not defined. Kindly help me out here because I have not been able to find a fix that works for me all over the forum and the web. See my codes belw:

RESUME RELATION FUNCTION




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, 'Users', 'user_id'),

			'resumeText' => array(self::BELONGS_TO, 'ResumeText', 'resumeTextID'),

		);

	}



RESUME TEXT RELATION FUNCTION




   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(

			'resume' => array(self::HAS_ONE, 'Resume', 'resumeID'),

		);

	}







DEFAULT CONTROLLER




  public function actionCv_details()

    {

		//$recruiter_id = Yii::app()->recruiter->getId();


		$uid = $_GET['uid'];

		//$model = Resume::model()->findByAttributes(array('user_id'=>$uid));


			


			$criteria = new CDbCriteria();		

			/*$criteria->with = array(

				 'resume_text'=>array('select'=>'resumeTextID'),				 

			);*/

			$criteria->condition = '(user_id=:user_id)';			

			$criteria->params = array(':user_id'=>$uid);		

			

			$dataProvider = new CActiveDataProvider('Resume',

					array(

							'criteria'  => $criteria,

							//'pagination' => array('pageSize' => 50,),

							//'totalItemCount' => 20,

					)

			 );

			

			$this->render('cv_details',array('dataProvider'=>$dataProvider));

	}




VIEW FILE




$models =$dataProvider->getData();

		

foreach($models as $data){

    echo CHtml::encode($data->resumeText->skills).'<br />'; 

    echo CHtml::encode($data->resumeText->experience).'<br />'; 

    echo CHtml::encode($data->resumeText->qualification).'<br />'; 

}






Thanks.

Try this:




$criteria->with = array('resumeText'=>array('select'=>'resumeTextID'));

$criteria->together = true;



Inside your Resume model, there is no relation called "resume_text" like you wrote here:




$criteria->with = array('resume_text'=>array('select'=>'resumeTextID'));



Thanks for your quick response duri,

I tried that and now I see this error:

Relation "resumeText" is not defined in active record class "Resume".

But it is. Why is it not recognising it?

I don’t understand why are you using dataprovider?

Try this code in your controller:

Using Eager loading




			//Eager loading


			$criteria->condition = '(user_id=:user_id)';  

			$criteria->params = array(':user_id'=>$uid);            

			$models=Resume::model()->with(array("resumeText"=>array("select"=>'resumeTextID')))->fetchAll($criteria);//Here you fetch all models from the DB

			 $this->render('cv_details',array('models'=>$models));//Send models to your view



If code above is not working, try this




//LAZY LOADING




			$criteria->condition = '(user_id=:user_id)';  

			$criteria->params = array(':user_id'=>$uid);            

			$models=Resume::model()->fetchAll($criteria);//Here you fetch all models from the DB

			 $this->render('cv_details',array('models'=>$models));//Send models to your view

Edit your view file like this:




$models =$dataProvider->getData();//Delete this line in your view file


foreach($models as $data){

    echo CHtml::encode($data->resumeText->skills).'<br />'; 

    echo CHtml::encode($data->resumeText->experience).'<br />'; 

    echo CHtml::encode($data->resumeText->qualification).'<br />'; 

}

If it is not working, please paste content of your Resume model and errors you get.

Thanks again duri,

I have tried both Its still not working. Both are showing the same error:

Relation "resumeText" is not defined in active record class "Resume".

Though, I made a correction to your code, it was supposed to be findAll() not fetchall(). I guess that was a mistake.

I have used both eager loading and easy loading previously but still having the same problem that was why i decided to try out dataProvider.

see my Resume model code below:




<?php


/**

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

 *

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

 * @property string $resumeID

 * @property string $cvID

 * @property string $user_id

 * @property string $firstName

 * @property string $middleName

 * @property string $lastName

 * @property string $email

 * @property string $phone

 * @property string $mobile

 * @property string $faxNo

 * @property string $address

 * @property string $city

 * @property string $state

 * @property string $country

 * @property string $zipCode

 * @property string $category

 * @property string $subCategory

 * @property string $dob

 * @property string $maritalStatus

 * @property string $gender

 * @property string $nationality

 * @property string $highestQualification

 * @property string $secondHighestQualification

 * @property string $skills

 * @property string $hobbies

 * @property string $languageKnown

 * @property integer $experienceYear

 * @property string $currentEmployer

 * @property string $jobProfile

 * @property string $cvURL

 * @property string $submitted_by

 * @property string $last_editted

 * @property string $dateSubmitted

 *

 * The followings are the available model relations:

 * @property Users $user

 */

class Resume extends CActiveRecord

{


	const TYPE_MALE = 'Male';

	const TYPE_FEMALE = 'Female';

	const TYPE_DECLINE = 'Decline to Identify';

	const TYPE_MARRIED = 'Married';

	const TYPE_SINGLE = 'Single';

	const TYPE_DIVORCED = 'Divorced';

	const TYPE_WIDOWED = 'Widowed';

	const TYPE_SCHOOL_QUALIFICATION = 'School Qualification';

	const TYPE_VOCATIONAL_APPRENTICESHIP = 'Vocational/Apprenticeship';

	const TYPE_COLLEGEDIPLOMA_CERTIFICATE = 'College Diploma/Certificate';

	const TYPE_FIRST_DEGREE = 'First Degree';	

	const TYPE_MASTER_DEGREE = 'Master Degree';

	const TYPE_MBA = 'MBA';

	const TYPE_PHD = 'PHD';	

	const TYPE_OND = 'OND';

	const TYPE_HND = 'HND';	

	

	//public $category_name = '';


	public $detailedResume;

    

 

	

	/**

	* Retrieves a list of Highest Qualifications types

	* @return array an array of available qualification types.

	*/

		public function getHQOptions()

		{

			return array(

				self::TYPE_SCHOOL_QUALIFICATION => 'School Qualification',

				self::TYPE_VOCATIONAL_APPRENTICESHIP => 'Vocational/Apprenticeship',

				self::TYPE_COLLEGEDIPLOMA_CERTIFICATE => 'College Diploma/Certificate',

				self::TYPE_FIRST_DEGREE => 'First Degree',				

				self::TYPE_MASTER_DEGREE => 'Master Degree',

				self::TYPE_MBA => 'MBA',

				self::TYPE_PHD => 'PHD',	

				self::TYPE_MBA => 'OND',

				self::TYPE_PHD => 'HND',

			);

		}




	/**

	* Retrieves a list of Marital types

	* @return array an array of available role types.

	*/

		public function getMaritalOptions()

		{

			return array(

				self::TYPE_DECLINE => 'Decline to Identify',

				self::TYPE_MARRIED => 'Married',

				self::TYPE_SINGLE => 'Single',

				self::TYPE_DIVORCED => 'Divorced',

				self::TYPE_WIDOWED => 'Widowed',

			);

		}




	/**

	* Retrieves a list of role types

	* @return array an array of available role types.

	*/

		public function getGenderOptions()

		{

			return array(

				self::TYPE_MALE=>'Male',

				self::TYPE_FEMALE=>'Female',

			);

		}

	


	/**

	* Retrieves a the selected Gender Type From the Database

	*/

		public function getSelectedGenderOption()

		{


			$criteria = new CDbCriteria();

			$criteria->select = 'gender';

			$criteria->condition = 'user_id=:user_id';

			$criteria->params = array(':user_id'=>Yii::app()->user->id);

			$model = $this -> find($criteria);

	

			return  CHtml::encode($model->gender);			

			

		}


			/**

	* Retrieves a the selected Industry Type From the Database

	*/

		public function getSelectedIndustryOption()

		{


			$criteria = new CDbCriteria();

			$criteria->select = 'category';

			$criteria->condition = 'user_id=:user_id';

			$criteria->params = array(':user_id'=>Yii::app()->user->id);

			$model = Resume::model()->find($criteria);

	

			return  CHtml::encode($model->category);			

			

		}


		/**

	* Retrieves a the selected Specialization Type From the Database

	*/

		public function getSelectedSpecializationOption()

		{


			$criteria = new CDbCriteria();

			$criteria->select = 'subCategory';

			$criteria->condition = 'user_id=:user_id';

			$criteria->params = array(':user_id'=>Yii::app()->user->id);

			$model = Resume::model()->find($criteria);

	

			return  CHtml::encode($model->subCategory);			

			

		}

	

	

	/**

	* Retrieves a the selected Marital Type From the Database

	*/

		public function getSelectedMaritalOption()

		{


			$criteria = new CDbCriteria();

			$criteria->select = 'maritalStatus';

			$criteria->condition = 'user_id=:user_id';

			$criteria->params = array(':user_id'=>Yii::app()->user->id);

			$model = $this -> find($criteria);

	

			return  CHtml::encode($model->maritalStatus);			

			

		}


	/**

	* Retrieves a the selected HQ Type From the Database

	*/

		public function getSelectedHQOption()

		{


			$criteria = new CDbCriteria();

			$criteria->select = 'highestQualification';

			$criteria->condition = 'user_id=:user_id';

			$criteria->params = array(':user_id'=>Yii::app()->user->id);

			$model = $this -> find($criteria);

	

			return  CHtml::encode($model->highestQualification);			

			

		}


		/**

	* Retrieves a the selected HQ Type From the Database

	*/

		public function getSelectedSHQOption()

		{


			$criteria = new CDbCriteria();

			$criteria->select = 'secondHighestQualification';

			$criteria->condition = 'user_id=:user_id';

			$criteria->params = array(':user_id'=>Yii::app()->user->id);

			$model = $this -> find($criteria);

	

			return  CHtml::encode($model->secondHighestQualification);			

			

		}




		/**

	* Retrieves a list of role range values

	* @return array an array of role range values.

	*/

		public function getAllowedGenderRange()

		{

			return array(

				self::TYPE_MALE,

				self::TYPE_FEMALE,

			);

		}


	

		/**

	* Retrieves a list of role range values

	* @return array an array of role range values.

	*/

		public function getAllowedMaritalRange()

		{

			return array(

				self::TYPE_DECLINE,

				self::TYPE_MARRIED,

				self::TYPE_SINGLE,

				self::TYPE_DIVORCED,

				self::TYPE_WIDOWED,

			);

		}


	/**

	* Retrieves a list of role range values

	* @return array an array of role range values.

	*/

		public function getAllowedHQRange()

		{

			return array(

				self::TYPE_SCHOOL_QUALIFICATION,

				self::TYPE_VOCATIONAL_APPRENTICESHIP,

				self::TYPE_COLLEGEDIPLOMA_CERTIFICATE,

				self::TYPE_FIRST_DEGREE,				

				self::TYPE_MASTER_DEGREE,

				self::TYPE_MBA,

				self::TYPE_PHD,	

				self::TYPE_OND,

				self::TYPE_HND,	

			);

		}

		

	

	/**

	* Retrieves category name values

	* @return array an array of role range values.

	*/

		public function getCategoryName()

		{

			return  CHtml::encode($this->category);	

			//return $this->category;

		}

	


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'resume';

	}


	/**

	 * @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('firstName, lastName, email, phone, address, city, state, country, category, subCategory, dob, maritalStatus, gender, nationality, highestQualification,skills, hobbies, languageKnown, experienceYear, currentEmployer, jobProfile', 'required'),

			array('email', 'email'),

			array('dob', 'date', 'format'=>'yyyy/M/d'),

			array('subCategory','safe'),

			array('experienceYear, phone, mobile', 'numerical', 'integerOnly'=>true),

			array('firstName, middleName, lastName, email, address, city, state, country, category, subCategory, dob, maritalStatus, gender, nationality, highestQualification, secondHighestQualification, hobbies, languageKnown, currentEmployer, jobProfile', 'length', 'max'=>255),

			array('gender', 'in', 'range'=>self::getAllowedGenderRange()),

			array('maritalStatus', 'in', 'range'=>self::getAllowedMaritalRange()),

			array('highestQualification', 'in', 'range'=>self::getAllowedHQRange()),

			//array('user_id', 'length', 'max'=>11),

			array('phone, mobile, faxNo, zipCode', 'length', 'max'=>20),

			//array('submitted_by', 'length', 'max'=>5),

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

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

			array('resumeID, firstName, middleName, lastName, email, phone, mobile, faxNo, address, city, state, country, zipCode, category, subCategory, dob, maritalStatus, gender, nationality, highestQualification, secondHighestQualification, skills, hobbies, languageKnown, experienceYear, currentEmployer, jobProfile, submitted_by, last_editted, dateSubmitted', '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, 'Users', 'user_id'),

			'resumeText' => array(self::BELONGS_TO, 'ResumeText', 'resumeTextID'),

		);

	}


	/**

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

	 */

	public function attributeLabels()

	{

		return array(					

			'firstName' => 'First Name',

			'middleName' => 'Middle Name',

			'lastName' => 'Last Name',

			'email' => 'Email',

			'phone' => 'Phone',

			'mobile' => 'Mobile No',

			'faxNo' => 'Fax No',

			'address' => 'Address',

			'city' => 'City',

			'state' => 'State',

			'country' => 'Country',

			'zipCode' => 'Zip Code',

			'category' => 'Industry',

			'subCategory' => 'Specialization',

			'dob' => 'Date of Birth',

			'maritalStatus' => 'Marital Status',

			'gender' => 'Gender',

			'nationality' => 'Nationality',

			'highestQualification' => 'Highest Qualification',

			'secondHighestQualification' => 'Second Highest Qualification',

			'skills' => 'Skills',

			'hobbies' => 'Hobbies',

			'languageKnown' => 'Language Known',

			'experienceYear' => 'Years of Experience',

			'currentEmployer' => 'Current Employer',

			'jobProfile' => 'Job Profile',

			'cvURL' => 'Cv Url',			

		);

	}


	/**

	 * Retrieves a list of models based on the current search/filter conditions.

	 *

	 * Typical usecase:

	 * - Initialize the model fields with values from filter form.

	 * - Execute this method to get CActiveDataProvider instance which will filter

	 * models according to data in model fields.

	 * - Pass data provider to CGridView, CListView or any similar widget.

	 *

	 * @return CActiveDataProvider the data provider that can return the models

	 * based on the search/filter conditions.

	 */

	public function search()

	{

		// @todo Please modify the following code to remove attributes that should not be searched.


		$criteria=new CDbCriteria;


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}


	/**

	 * Returns the static model of the specified AR class.

	 * Please note that you should have this exact method in all your CActiveRecord descendants!

	 * @param string $className active record class name.

	 * @return Resume the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}




}




Hope its not too long?

Thanks.

Are you sure you generated the following models:

  • [size=2]ResumeText[/size]
  • [size=2]Users[/size]

In this post http://www.yiiframework.com/forum/index.php/topic/12100-cactivedataprovider-and-with-conditon/ user had the similar issue.

Which version of Yii framework are you using?

Yes I generated it from gii.

I am using version v1.1.14.