Relation active record - Trying to get property of non-object,

User table has relation of

‘userpersonaldetails’ => array(self::HAS_ONE, ‘Userpersonaldetails’, ‘userId’),

Userpersonaldetails has relation of

                    'religion' => array(self::HAS_MANY, 'Religion', 'religionId'),


		'country' => array(self::HAS_MANY, 'Country', 'countryId'),

Religion and country has field of name

When i tried to fetch the name like $user->userpersonaldetails->country->name its showing error Trying to get property of non-object,however when i try to print the value $user->userpersonaldetails->country it has all records - I am dumping the value of $user->userpersonaldetails->country

I couldnt find where i missed somethign or some mistakes in the code… please helpe me out

array(1) { [0]=> object(Country)#111 (12) { ["_md":“CActiveRecord”:private]=> object(CActiveRecordMetaData)#100 (5) { [“tableSchema”]=> object(CMysqlTableSchema)#101 (9) { [“schemaName”]=> NULL [“name”]=> string(7) “country” [“rawName”]=> string(9) “country” [“primaryKey”]=> string(9) “countryId” [“sequenceName”]=> string(0) “” [“foreignKeys”]=> array(0) { } [“columns”]=> array(3) { [“countryId”]=> object(CMysqlColumnSchema)#102 (14) { [“name”]=> string(9) “countryId” [“rawName”]=> string(11) “countryId” [“allowNull”]=> bool(false) [“dbType”]=> string(10) “bigint(20)” [“type”]=> string(6) “string” [“defaultValue”]=> NULL [“size”]=> int(20) [“precision”]=> int(20) [“scale”]=> NULL [“isPrimaryKey”]=> bool(true) [“isForeignKey”]=> bool(false) [“autoIncrement”]=> bool(true) ["_e":“CComponent”:private]=> NULL ["_m":“CComponent”:private]=> NULL } [“name”]=> object(CMysqlColumnSchema)#104 (14) { [“name”]=> string(4) “name” [“rawName”]=> string(6) “name” [“allowNull”]=> bool(false) [“dbType”]=> string(12) “varchar(250)” [“type”]=> string(6) “string” [“defaultValue”]=> string(0) “” [“size”]=> int(250) [“precision”]=> int(250) [“scale”]=> NULL [“isPrimaryKey”]=> bool(false) [“isForeignKey”]=> bool(false) [“autoIncrement”]=> bool(false) ["_e":“CComponent”:private]=> NULL ["_m":“CComponent”:private]=> NULL } [“active”]=> object(CMysqlColumnSchema)#105 (14) { [“name”]=> string(6) “active” [“rawName”]=> string(8)active” [“allowNull”]=> bool(false) [“dbType”]=> string(10) “bigint(20)” [“type”]=> string(6) “string” [“defaultValue”]=> string(1) “1” [“size”]=> int(20) [“precision”]=> int(20) [“scale”]=> NULL [“isPrimaryKey”]=> bool(false) [“isForeignKey”]=> bool(false) [“autoIncrement”]=> bool(false) ["_e":“CComponent”:private]=> NULL ["_m":“CComponent”:private]=> NULL } } ["_e":“CComponent”:private]=> NULL ["_m":“CComponent”:private]=> NULL } [“columns”]=> array(3) { [“countryId”]=> object(CMysqlColumnSchema)#102 (14) { [“name”]=> string(9) “countryId” [“rawName”]=> string(11) “countryId” [“allowNull”]=> bool(false) [“dbType”]=> string(10) “bigint(20)” [“type”]=> string(6) “string” [“defaultValue”]=> NULL [“size”]=> int(20) [“precision”]=> int(20) [“scale”]=> NULL [“isPrimaryKey”]=> bool(true) [“isForeignKey”]=> bool(false) [“autoIncrement”]=> bool(true) ["_e":“CComponent”:private]=> NULL ["_m":“CComponent”:private]=> NULL } [“name”]=> object(CMysqlColumnSchema)#104 (14) { [“name”]=> string(4) “name” [“rawName”]=> string(6) “name” [“allowNull”]=> bool(false) [“dbType”]=> string(12) “varchar(250)” [“type”]=> string(6) “string” [“defaultValue”]=> string(0) “” [“size”]=> int(250) [“precision”]=> int(250) [“scale”]=> NULL [“isPrimaryKey”]=> bool(false) [“isForeignKey”]=> bool(false) [“autoIncrement”]=> bool(false) ["_e":“CComponent”:private]=> NULL ["_m":“CComponent”:private]=> NULL } [“active”]=> object(CMysqlColumnSchema)#105 (14) { [“name”]=> string(6) “active” [“rawName”]=> string(8)active” [“allowNull”]=> bool(false) [“dbType”]=> string(10) “bigint(20)” [“type”]=> string(6) “string” [“defaultValue”]=> string(1) “1” [“size”]=> int(20) [“precision”]=> int(20) [“scale”]=> NULL [“isPrimaryKey”]=> bool(false) [“isForeignKey”]=> bool(false) [“autoIncrement”]=> bool(false) ["_e":“CComponent”:private]=> NULL ["_m":“CComponent”:private]=> NULL } } [“relations”]=> array(1) { [“states”]=> object(CHasManyRelation)#106 (22) { [“limit”]=> int(-1) [“offset”]=> int(-1) [“index”]=> NULL [“through”]=> NULL [“joinType”]=> string(15) “LEFT OUTER JOIN” [“on”]=> string(0) “” [“alias”]=> NULL [“with”]=> array(0) { } [“together”]=> NULL [“scopes”]=> NULL [“name”]=> string(6) “states” [“className”]=> string(6) “States” [“foreignKey”]=> string(9) “countryId” [“select”]=> string(1) “*” [“condition”]=> string(0) “” [“params”]=> array(0) { } [“group”]=> string(0) “” [“join”]=> string(0) “” [“having”]=> string(0) “” [“order”]=> string(0) “” ["_e":“CComponent”:private]=> NULL ["_m":“CComponent”:private]=> NULL } } [“attributeDefaults”]=> array(2) { [“name”]=> string(0) “” [“active”]=> string(1) “1” } ["_model":“CActiveRecordMetaData”:private]=> object(Country)#69 (12) { ["_md":“CActiveRecord”:private]=> RECURSION ["_new":“CActiveRecord”:private]=> bool(false) ["_attributes":“CActiveRecord”:private]=> array(0) { } ["_related":“CActiveRecord”:private]=> array(0) { } ["_c":“CActiveRecord”:private]=> NULL ["_pk":“CActiveRecord”:private]=> NULL ["_alias":“CActiveRecord”:private]=> string(1) “t” ["_errors":“CModel”:private]=> array(0) { } ["_validators":“CModel”:private]=> NULL ["_scenario":“CModel”:private]=> string(0) “” ["_e":“CComponent”:private]=> NULL ["_m":“CComponent”:private]=> NULL } } ["_new":“CActiveRecord”:private]=> bool(false) ["_attributes":“CActiveRecord”:private]=> array(3) { [“countryId”]=> string(1) “1” [“name”]=> s[color="#FF0000"][color="#FFFF00"]tring(5) “India” [/color][/color][“active”]=> string(1) “1” } ["_related":“CActiveRecord”:private]=> array(0) { } ["_c":“CActiveRecord”:private]=> NULL ["_pk":“CActiveRecord”:private]=> string(1) “1” ["_alias":“CActiveRecord”:private]=> string(1) “t” ["_errors":“CModel”:private]=> array(0) { } ["_validators":“CModel”:private]=> NULL ["_scenario":“CModel”:private]=> string(6) “update” ["_e":“CComponent”:private]=> NULL ["_m":“CComponent”:private]=> NULL } }

Dear Friend,

By seeing your declaration of relations,

I perceive, every Userpersonaldetails has many country objects.

$user->userpersonaldetails->country is going to bring an array of objects.

In order to fetch the name propert of each country object. You have to use the loop.




foreach($user->userpersonaldetails->country as $country)

   {

      echo $country->name;

   }



Do you know a user that has many religions and many countries?