我有一个设置,其中一个模型我有重复字段,如div#image-wrap {
background-color: white;
padding: 0;
margin: auto;
text-align: center;
}
div.image-column {
width: 31.5%;
background-color: white;
display: inline-block;
margin: 0px;
}
.image-container{
position: relative;
display: flex;
margin-top: 10px;
text-align:center;
}
.image-container img {
width: 100%;
transition: .5s ease;
}
.image-container:hover img {
opacity: .6;
transition: .5s ease;
}
#img-name {
position: absolute;
left: 0; /* stretch the text container */
right: 0; /* stretch the text container */
transition: .5s ease;
display: flex;
align-self: center;
text-align: center;
}
#img-name:hover {
opacity: 1;
transition: .5s ease;
}
p {
margin: 0px;
font-size: 20px;
text-align: center; /* to make the text in the center */
color: #fff; /* to make the text white */
font-size: 1.5vw;
width: 100%;
text-shadow: 0 0 2px #000;
z-index: 999;
}
和<div id="image-wrap">
<div class="image-column" id="col-1">
<div class="image-container">
<div id="img-name">
<p>Pier To Pier, Brighton Pier</p>
</div>
<img src="https://loremflickr.com/100/100/dog">
</div>
<div class="image-container">
<div id="img-name">
<p>Looking Back, Brighton Beach</p>
</div>
<img src="https://loremflickr.com/100/100/cat">
</div>
<div class="image-container">
<div id="img-name">
<p>Heart Shaped Flocks, Brighton Pier</p>
</div>
<img src="https://loremflickr.com/100/100/flower">
</div>
</div>
</div>
所以user
有一组字段,例如userchildren
,userchildren
和child_name
我的控制器代码是这样的:
child_birth_date
我面临两个问题:
首先,当我要更新配置文件时,数据在表单中正确显示,我可以向模型添加没有任何问题的记录child_gender
但如果我编辑现有记录,它会将其作为新记录插入,而我希望这会更新。
我有这样的代码,如下所示:
,
public function actionUpdateProfile()
{
$user_id = Yii::$app->user->identity->id;
$model = User::find()->where(['id' => $user_id])->one();
$UserProfile = UserProfile::find()->where(['user_id' => $model->id])->one();
$userbillinginfo = UserBillingInfo::find()->where(['user_id' => $model->id])->one();
$userchildren = UserChildren::find()->where(['user_id' => $model->id])->all();
if ($userchildren) {
$profile = $UserProfile;
$billinginfo = $userbillinginfo;
$userchild = $userchildren;
} else {
$profile = new UserProfile;
$profile->user_id = $model->id;
$billinginfo = new UserBillingInfo;
$billinginfo->user_id = $model->id;
$userchild = New UserChildren;
$userchild->user_id = $model->id;
}
if (Yii::$app->request->isAjax && $model->load($_POST)) {
Yii::$app->response->format = 'json';
return \yii\bootstrap\ActiveForm::validate($model);
}
if (Yii::$app->request->isAjax && $profile->load($_POST)) {
Yii::$app->response->format = 'json';
return \yii\bootstrap\ActiveForm::validate($profile);
}
if (Yii::$app->request->isAjax && $billinginfo->load($_POST)) {
Yii::$app->response->format = 'json';
return \yii\bootstrap\ActiveForm::validate($billinginfo);
}
if (Yii::$app->request->isAjax && $userchild->load($_POST)) {
Yii::$app->response->format = 'json';
return \yii\bootstrap\ActiveForm::validate($userchild);
}
if ($model->load(Yii::$app->request->post()) && $profile->load(Yii::$app->request->post()) && $billinginfo->load(Yii::$app->request->post())) {
$model->username = $model->email;
$model->save();
$profile->save();
$billinginfo->save();
if (!empty($_POST['UserChildren']) && !is_null($_POST['UserChildren'])) {
foreach ($_POST['UserChildren'] as $rows) {
$userchild = New UserChildren;
$userchild->attributes = $rows;
$userchild->user_id = $model->id;
$userchild->save();
}
}
return $this->redirect(['view']);
} else {
return $this->render('update-profile', [
'model' => $model,
'profile' => $profile,
'billinginfo' => $billinginfo,
'userchild' => $userchild,
]);
}
}
但在我更新了代码行之后
UserChildren
到
if ($model->load(Yii::$app->request->post()) and
$profile->load(Yii::$app->request->post()) and
$billinginfo->load(Yii::$app->request->post()) and
$billinginfo->load(Yii::$app->request->post())
) {
...
因此必须修改如下所示的行:
$userchildren = UserChildren::find()->where(['user_id' => $model->id])->one();
我在这里缺少什么?感谢。