Relational AR not working - Property "XX" is not defined.

Hey,

just started using relational AR.

However it is not working:

I get the following error:


 Property "user.cc" is not defined.  

The Property is defined in country.cc and not in user.cc

Here is the User-Controller Snippet




	public function actionView($id)

	{

		$myModel = $this->loadModel($id);

		$test = $myModel->countryUser;

		print $test;


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

			'model'=>$myModel,

		));

	}



Here is the User-Model Snippet




	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(

			// choose			       ModelName   KeyName

			'countryUser' => array(self::BELONGS_TO, 'country', 'cc'),

		);

	}



And here the relevant DB Table Definitions




CREATE  TABLE IF NOT EXISTS `dfg`.`tb_user` (

  `idUser` INT NOT NULL AUTO_INCREMENT ,

  `username` VARCHAR(45) NOT NULL ,

  `country` VARCHAR(45) NULL ,

  `state` VARCHAR(45) NULL ,

  PRIMARY KEY (`idUser`) ,

  INDEX `countryUser` (`country` ASC) ,

  CONSTRAINT `countryUser`

    FOREIGN KEY (`country` )

    REFERENCES `dfg`.`tb_geo_country` (`cc` )

    ON DELETE NO ACTION

    ON UPDATE NO ACTION)

ENGINE = InnoDB;





CREATE  TABLE IF NOT EXISTS `dfg`.`tb_geo_country` (

  `cc` VARCHAR(2) NOT NULL ,

  `country` TEXT NULL ,

  PRIMARY KEY (`cc`) )

ENGINE = InnoDB;












You need to make it a HAS_ONE or switch your key to be country. When you use BELONGS_TO it you pass in the attribute that the current model has which is keyed to the other model.

Does that make sense?

Seems like you use the wrong foreign key name. It’s country, not cc in your user table.

Yeah thx!

That solved it…

Finally understand relational AR