I'm prety new to yii framework so i could use some help with something. Lets say i got a table in my database with users, 1 of the profile fields is nationality. In another table i got a lot of nationalities like this:
id Nationality short
1 Germany DE
2 France FR
3 Netherlands NL
etc... about 60+ nationalities. In the usertable, the user nationality is linked with this table, like this:
id username nationality
1 user 1
that means the nationality of user 1 = germany. but how can i select germany from the nationalities table when im in the profile view?
i hope im clear, else ask :)
greets, stefan.
You need model classes for both tables, eg
class Profile extends CActiveRecord {
...
}
class Nationality extends CActiveRecord {
...
}
Then in the Profile
model you need a relation to Nationality
:
public function relations() {
return array(
'nationality' => array(self::BELONGS_TO, 'Nationality', 'nationality'),
),
}
The name of the relation 'nationality'
is how you refer to the related model, eg:
$profile = Profile::model()->findByPk($id);
echo $profile->nationality->short;
Tahts what relations are for: see relational model :
'nationality' => array(self::BELONGS_TO, 'Nationality', 'nationality')
Amd in your view, if you are using a CDetailView you can reference the nationality by its relation name, and call its attributes with dot notation:
nationality.short
read the CDetailView docs for more examples
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.