簡體   English   中英

Yii2 $ model-> load(Yii :: $ app-> request-> post())不會從表單加載數據

[英]Yii2 $model->load(Yii::$app->request->post()) does not load data from form

當我取出要加載數據的條件時,將其保存到db,$ _POST獲得值,但不將其發送到controller,這種方式在我的其他項目中有效,但在這里不行。 如果我使用if(isset($_POST['money']) && isset($_POST['username'])){保存數據,它將起作用,但load()函數無效。

控制者

public function actionSend() {
    $model = new User();
    $model->getErrors();
    if ($model->load(Yii::$app->request->post())) {
        $model->money = 'something';
        $model->username = 'something';
        $model->save();
    }
    return $this->render('send', [
        'model' => $model
    ]);
}

模型

<?php

namespace app\models;
use yii\db\ActiveRecord;

use Yii;

class User extends \yii\db\ActiveRecord {
    public static function tableName() {
        return 'user';
    }

    public function rules() {
        return [
            [['username', 'money'], 'safe'],
            [['username', 'password'], 'string', 'max' => 15],
            [['auth_key', 'access_token'], 'string', 'max' => 32],
            [['money'], 'string', 'max' => 8],
        ];
    }

    public function attributeLabels() {
        return [
            'id' => 'ID',
            'username' => 'Username',
            'password' => 'Password',
            'auth_key' => 'Auth Key',
            'access_token' => 'Access Token',
            'money' => 'Money',
        ];
    }
}

視圖

<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
?>

<h2>Send</h2>

<?php $form = ActiveForm::begin([
    'layout' => 'horizontal',
    'fieldConfig' => [
        'template' => "{label}\n<div class=\"col-lg-3\">{input}</div>\n<div class=\"col-lg-8\">{error}</div>",
        'labelOptions' => ['class' => 'col-lg-1 control-label'],
    ],
]); ?>
    <?= $form->field($model, 'username')->textInput(['name' => 'username']) ?>

    <?= $form->field($model, 'money')->textInput(['name' => 'money'])?>

    <div class="form-group">
        <div class="col-lg-offset-1 col-lg-11">
            <?= Html::submitButton('Send', ['class' => 'btn btn-success']) ?>
        </div>
    </div>

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

將您的控制器更改為此

public function actionSend() {
    $model = new User();
    $model->getErrors();

    /* set the second parameters of load to empty string */
    if ($model->load(Yii::$app->request->post(), '')) {
        $model->money = 'something';
        $model->username = 'something';
        $model->save();
    }
    return $this->render('send', [
        'model' => $model
    ]);
}

如果查看load方法的實現,您會發現load有兩個參數,第一個是要分配的數據,第二個是數據的前綴名稱。

讓我們看一個示例來說明第二個參數的用法。 (我們假設您的表單名是User

$data1 = [
    'username' => 'sam',
    'money' => 100
];

$data2 = [
    'User' => [
        'username' => 'sam',
        'money' => 100
    ],
],

// if you want to load $data1, you have to do like this
$model->load($data1, '');

// if you want to load $data2, you have to do like this
$model->load($data2);

// either one of the two ways, the result is the same.
echo $model->username;    // sam
echo $model->money;       // 100

希望對您有所幫助。

讓我們看下面的示例:使用mode值驗證對象或數組鍵

//CONVERT OBJECT TO ARRAY
        $model_data = \yii\helpers\ArrayHelper::toArray($json);

Array
(
    [device_id] => abcd
    [device_type] => android
    [c_id] => 38   
    [device_for] => rent   
    [area_id] => 1
    [city_id] => 1
)

讓我們將數組數據加載到模型中

//LOAD POST DATA IN MODEL
$model->setAttributes($model_data);



if ($model->validate()) {

}else{

}

暫無
暫無

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

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