簡體   English   中英

同一個ActiveForm上的多個模型yii2

[英]Multiple Model on same ActiveForm yii2

在同一個表單上有多個模型我不想在兩個模型上保存數據,但我只想在視圖文件上顯示一個變量

我有2個桌面OrganiserEvent Organiser將登錄和Create Event現在有幾個字段在兩個表中都很常見所以當登錄用戶點擊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

這是我的Event模型

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模型表示前端模型SignUp.php Organiser表和模型名稱

   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;
}

}

所以我想在字段locationCreate event表單上顯示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的情況下創建。 希望這會幫助你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM