gpt4 book ai didi

javascript - 同一个 ActiveForm yii2 上的多个模型

转载 作者:可可西里 更新时间:2023-11-01 13:26:01 26 4
gpt4 key购买 nike

在同一个表单上有多个模型我不想在两个模型上保存数据,但我只想在 View 文件上显示一个变量

我有两张 table 组织者事件Organiser 将登录并创建事件现在两个表中有几个共同的字段所以当登录用户点击 Create Event 按钮时,我想在表单上显示 Organiser 表中的 address

这是我到目前为止所做的这可能不是正确的方法所以让我知道是否可以通过任何其他更简单的方式实现这一目标

public function actionCreate()
{
$model = new Event();

$model2 = $this->findModel2(Yii::$app->user->id);


if ($model->load(Yii::$app->request->post())) {

if($_POST['User']['address'] == null)
{
$model->location = $model2->address;
}
else{
$model->location = $_POST['User']['address'];
}

$model->is_active = 1 ;

$model->save();


return $this->redirect(['view', 'id' => $model->id]);
} else {

return $this->render('create', [
'model' => $model,
'model2' => $model2
]);
}
}
protected function findModel2($id)
{
if (($model2 = User::findOne($id)) !== null) {
return $model2;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}

这是我的_form.php代码

 <?php $form = ActiveForm::begin([
'options' => [
'id' => 'create-event-form'
]
]); ?>


<?= $form->field($model, 'interest_id')->dropDownList(
ArrayHelper::map(Areaintrest::find()->all(),'id','area_intrest'),
['prompt'=> 'Select Event Category']
) ?>

<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
<?php
if ($model->isNewRecord){
echo $form->field($model2,'address')->textInput(['class' => 'placepicker form-control'])->label('Location');
}
else
echo $form->field($model,'location')->textInput(['class' => 'placepicker form-control']);

?>

<di"form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-danger' : 'btn btn-primary']) ?>
</div>

<?php ActiveForm::end(); ?>

现在这里的问题似乎是,在保存部分时抛出 errorunknown property address 因为数据库表 Event< 中不存在该字段我只想将该数据显示在我的 create form

的位置字段中

那我该怎么办

这是表结构

Organiser          Event 
organiser_id event_id
username organiser_id
clubname title
image location
email
firstname
lastname
address

错误页面是这样的

Unknown Property – yii\base\UnknownPropertyException

Getting unknown property: common\models\Event::address

这是我的事件模型

class Event extends \yii\db\ActiveRecord
{

public static function tableName()
{
return 'event';
}


public function rules()
{
return [
[['organiser_id', 'interest_id', 'title', 'location'], 'required'],
[['organiser_id', 'interest_id'], 'integer'],
[['title', 'location'], 'string', 'max' => 255],
];
}

/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'organiser_id' => 'Organiser ID',
'interest_id' => 'Event Type',
'title' => 'Event Title',
'location' => 'Location'

];
}


public function getOrganiser()
{
return $this->hasOne(User::className(), ['organiser_id' => 'organiser_id']);
}

这是我的 User 模型代表 Organiser 表和前端模型 SignUp.php 中的模型名称

   class SignupForm extends Model
{
public $username;
public $address;
public $password;
public $password_repeat;



public function rules()
{
return [
['username', 'filter', 'filter' => 'trim'],
['username', 'required'],
['address', 'required'],
['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],
['username', 'string', 'min' => 2, 'max' => 255],


[['file'], 'file', 'extensions'=>'jpg, gif, png'],



['password', 'required'],
['password', 'string', 'min' => 6],

['password_repeat', 'compare', 'compareAttribute' => 'password'],
];
}

public function attributeLabels()
{
return [
'password_repeat' => 'Confirm Password',
'address' => 'Address',
'username' => 'Username',
];
}



public function signup()
{
if ($this->validate()) {
$user = new User();
$model = new SignupForm();
$user->address = $this->address;
$user->username = $this->username;

$user->setPassword($this->password);
$user->generateAuthKey();
if ($user->save()) {
return $user;
}
}

return null;
}

所以我想在 Create event 表单的 location 字段中显示 organiser.address希望你现在能明白谢谢

最佳答案

可能你的函数应该是这样的

public function actionCreate()
{
$event= new Event();

$user= $this->findModel2(Yii::$app->user->id);
$event->location = $user->address;

if ($event->load(Yii::$app->request->post()) && $event->save()) {//active can set by default validator
return $this->redirect(['view', 'id' => $event->id]);
}
return $this->render('create', [
'event' => $event
]);

}

并且在表单中你总是显示位置。然后您可以在更新和创建中使用它,而无需查看 ifs。希望这会帮助你。

关于javascript - 同一个 ActiveForm yii2 上的多个模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32493945/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com