Is this a bug or just wrong usage?
I have this simple ActiveRecord class
class LocalizedPropertyValue extends ActiveRecord {
public function getLanguage() {
return $this->hasOne(Language::className(), ['id' => 'language']);
}
public static function tableName() {
return 'localized_property_value';
}
}
and this one
class Language extends ActiveRecord {
public static function tableName() {
return 'language';
}
}
with those tables (I think the foreign key can be ignored):
CREATE TABLE `language` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`)
CREATE TABLE `localized_property_value` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`value` VARCHAR(45) NOT NULL,
`language` INT(10) UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_localized_property_value_1_idx` (`language`),
CONSTRAINT `fk_localized_property_value_1` FOREIGN KEY (`language`) REFERENCES `localized_property_value` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION
)
If I call the language attribute of the LocalizedPropertyValue class I should get one Language object, right? But that’s not the case. When I call this:
$value = LocalizedPropertyValue::findOne();
$x = $value->language;
then $x will be an integer. It is the id of the found record.
Now, if I change the column name of localized_property_value from language to language_id then all works fine. I get the expected Language object ($x contains this object). Does that mean that there is a problem with Yii? Or is it required that a column name does not have the same name as a referenced table?
Took me hours to find out what the problem was. I hope that an answer saves some hours for other people if they encounter this issue…