php - Yii2 $model->load(Yii::$app->request->post()) 不从表单加载数据

标签 php database forms yii

当我取出条件来加载数据时,它会将其保存到数据库中,$_POST 获取值但不会将它们发送到 Controller ,这种方式适用于我的其他项目但不适用于此处。如果我使用 if(isset($_POST['money']) && isset($_POST['username'])){ 来保存数据它可以工作但不是 load() 函数。

Controller

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(); ?>

最佳答案

把你的 Controller 改成这个

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

希望对您有所帮助。

关于php - Yii2 $model->load(Yii::$app->request->post()) 不从表单加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47351260/

相关文章:

forms - 如何在奏鸣曲管理包中将 NULL 作为数据网格关系中的选项?

forms - symfony - Controller 中没有可更新的有效表单

php - 如何解决文件意外结束错误?

php - $facebook -> getUser() 返回错误值?

php - 在 db.php 中形成 post 数组到 class.php 和 PDO 插入函数

database - 如何在sqlplus中打开已经创建的数据库

java - 在 db hibernate 中插入外键

forms - GRAILS 2.0上的表单编码问题

php - 使用 Predis,如何设置(即存储)多维关联数组?

php - Android Php Mysqli : Response cannot be parsed as JSON data