:Yii2要求ポスト私は以下のコードが正しいと思う私のコントローラ内に次のコードを使用したいと思っています
$user = User::findOne(['username'=>$username]);
$profile = UserProfile::findOne(['user_id' => $user->id]);
if ($user->load(Yii::$app->request->post('User')) && $profile->load(Yii::$app->request->post('UserProfile'))) {
それは動作しません。しかし、およびまっすぐ私はそれ以下のif文の中にコードを実行使用があれば、以下のelseステートメントに行く:
if ($user->load(Yii::$app->request->post('_csrf')))
あなたはユーザー情報を見ることができるとのUserProfileがそうであるように、私は以下の通りです次の出力を得るのvar_dumpを使用して内に入れ子になっているのは正しいですか?動作するように最初の行:あなたが投稿した最初の行で
array(3) { ["_csrf"]=> string(56) "dsadasdasdasdas==" ["UserProfile"]=> array(3) { ["avatar"]=> string(0) "" ["social"]=> array(3) { ["facebook"]=> string(0) "" ["twitter"]=> string(0) "" ["instagram"]=> string(0) "" } ["bio"]=> string(5) "hello" } ["User"]=> array(3) { ["username"]=> string(22) "fsdfsdfsadminjkhnbnmbb" ["email"]=> string(23) "[email protected]" ["password"]=> string(14) "web16prounibar" } }
全コントローラアクション
public function actionProfile($username)
{
if (!Yii::$app->user->isGuest && $username == Yii::$app->user->identity->username){
$user = User::findOne(['username'=>$username]);
$profile = UserProfile::findOne(['user_id' => $user->id]);
// load user data with role and validate them
if ($user->load(Yii::$app->request->post()) && $profile->load(Yii::$app->request->post())) {
// only if user entered new password we want to hash and save it
if ($user->password)
{
$user->setPassword($user->password);
}
$user->save(false);
if ($profile->avatar){
$imageName = Yii::$app->security->generateRandomString() . '-' . $user->username;
$profile->file = UploadedFile::getInstance($profile, 'avatar');
$profile->filename = $imageName.'.'.$profile->file->extension;
$profile->file->saveAs('uploads/avatars/'.$imageName.'.'.$profile->file->extension);
$profile->avatar = '/uploads/avatars/'.$imageName.'.'.$profile->file->extension;
}
$profile->save(false);
return $this->goHome();
}else{
return $this->render('update', [
'user' => $user,
'profile' => $profile,
]);
}
}else{
return $this->goHome();
}
}
謝罪を私は現在、正しいコードを使っている最初のコードスニペットを更新しましたが、それはどちらの作業でもありません –
心配しないでください。すべてのクエリ(findOne)がアクティブなレコードを返しますか? –
私は手動で$ profile-> bio = "test"を実行しない限り、ユーザーモデルが更新されているが、userprofileモデルではなくリクエスト投稿内から名前を削除すると、両方ともレコードを返す。例: –