Yii 2 $model->load(Yii::$app->request->post())无法从表单中加载数据

92vpleto  于 2022-11-09  发布在  其他
关注(0)|答案(2)|浏览(278)

当我取出条件来加载数据时,它会将数据保存到数据库中,$_POST会获取值,但不会将它们发送到控制器,这种方式在我的其他项目中有效,但在这里无效。如果我使用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(); ?>
fzwojiic

fzwojiic1#

将控制器更改为

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有两个参数,第一个是要赋值的数据,第二个是数据的前缀名。
让我们看一个例子来说明第二个参数的用法。(我们假设您的formname是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

希望能有所帮助。

yhxst69z

yhxst69z2#

让我们看看下面的例子:使用模式值验证Objects或Array键

//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{

}

相关问题