同一活动形式的多重模型

时间:2022-11-24 20:20:25

have multiple model on same form I dont want to save data on both model but i just want to display one variable on view file

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

I have 2 tables Organiser and Event Organiser will log in and Create Event Now there are few fields which are common in both tables so when logged in user click on Create Event button i want to display address from Organiser table on the form

我有两个表格组织者和事件组织者将登录并创建事件现在两个表格中都有一些常见的字段,所以当用户登录时点击创建事件按钮,我想在表格中显示来自组织者表格的地址

This is what i have done until now which may not be the right approach so let me know if that can be achieved any other simpler way

这是我到目前为止所做的,这可能不是正确的方法,所以请让我知道,如果这可以实现任何其他更简单的方法

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.');
    }
}

And here is my _form.php code

这是我的_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(); ?>

Now the problem here seems is that when it comes to saving part throws error saying unknown property address because that field doesnt exists in the database table Event i just want that data to display it on the location field of my create form

这里的问题是,当保存部分时抛出错误,说未知属性地址,因为这个字段在数据库表事件中不存在,我只希望数据显示在create表单的location字段中

so what should i do here

我应该怎么做呢。

Here is the table structure

这是表格结构

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

Error page says something like this

错误页面上写着这样的东西。

Unknown Property – yii\base\UnknownPropertyException

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

And here is my Event model

这是我的事件模型

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']);
}

}

}

Here is my User model represent Organiser table and model name in frontend model SignUp.php

这是我的用户模型在frontend model .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;
}

}

}

So i want to display the organiser.address on the Create event form in the field location Hope you can understand it now thank you

所以我想展示组织者。在现场位置的Create事件表单上的地址希望您现在能理解,谢谢

1 个解决方案

#1


3  

May be your function should look like

你的功能应该是什么样的?

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
    ]);

}

And in form you show location always. Then you can use it in update and create as well without ifs in view. Hope this will help you.

在形式上,你总是显示位置。然后您可以在更新和创建中使用它,而不考虑ifs。希望这能对你有所帮助。

#1


3  

May be your function should look like

你的功能应该是什么样的?

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
    ]);

}

And in form you show location always. Then you can use it in update and create as well without ifs in view. Hope this will help you.

在形式上,你总是显示位置。然后您可以在更新和创建中使用它,而不考虑ifs。希望这能对你有所帮助。