UserとUserProfileの2つのモデルがあります
Userモデル内で、次の関係を定義しました。
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(
'userProfile' => array(self::HAS_ONE, 'UserProfile', 'user_id'),
);
}
UserProfileで、この関係を定義しました。
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, 'User', 'user_id'),
);
}
コントローラで次のコードを実行すると、次のようになります。
$user = User::model()->with('userProfile')->findByPK($userId);
$userProfile = $user->userProfile;
print_r($userProfile);
$userProfile変数がnullです。私はデータベースとコードをチェックして再チェックしました。Yiiのドキュメントも読み直しましたが、すべてが本来あるべき姿であるようです。しかし、それはただ働くことを拒否します!
私が間違っていることは何ですか?