Yii2活动记录模式不保存数据 [英] Yii2 active record model not saving data

查看:192
本文介绍了Yii2活动记录模式不保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了一个简单的表单模型和放大器;查看,一个简单的AR模型,和一个简单的控制器。该表单模型分配正确的值到AR实例,但是当我打电话保存(),这些都不值保存在数据库中。任何想法?

该表单模型:

 < PHP

命名空间的应用程序\车型;

使用Yii的;
使用警予\基地\型号;

类PromptForm扩展模式
{
    公众$名称;
    公众$介绍;
    公众$提示符;
    公众$说明;
    公众$的问题;


    公共职能attributeLabels()
    {
        返回 [
            '名'=> 提示标题',
            '介绍'=> 简介,
            '提示'=> 提示身体,
            '笔记'=> 关闭笔记,
            '问题'=> 探索的问题,
        ]。
    }

    / **
     * @返回数组的验证规则。
     * /
    公共职能规则()
    {
        返回 [
            [['名','提示'],'需要'],
            ['名','过滤器','过滤器'=> '修剪'],
            ['名','弦','最大'=> 255]
            ['前奏','提示','注意','问题'],'默认'],
        ]。
    }

    公共职能后()
    {
        如果($这 - >验证()){
            $提示符=新的提示();
            $ prompt-> NAME = $这个 - >名称;
            $ prompt->介绍= $这个 - >介绍;
            $ prompt->提示= $这个 - >提示;
            $ prompt->票据= $这个 - >说明;
            $ prompt->的问题= $这个 - >的问题;

            $ prompt->作者= \的Yii :: $ APP->用户自>的getId();

            //模(的print_r($提示符,TRUE));

            $ prompt->保存();

            返回$提示;
        }

        返回null;
    }
}
 

AR模型:

 < PHP

命名空间的应用程序\车型;

使用Yii的;
使用Yii的\ DB \ ActiveRecord的;

/ **
 *提示的背后是迅速的项目模型。
 * /
一流的提示扩展的ActiveRecord
{
    公众$名称;
    公众$介绍;
    公众$提示符;
    公众$说明;
    公众$的问题;
    公众$地位;
    公众$作者;

    公众的$ id;

    / **
     *本ActiveRecord类相关联的表@返回字符串的名称。
     * /
    公共静态函数tableName值()
    {
        返回提示;
    }

    / **
     * @返回数组的属性标签。
     * /
    公共职能attributeLabels()
    {
        返回 [
            '名'=> 提示标题',
            '介绍'=> 简介,
            '提示'=> 提示身体,
            '笔记'=> 关闭笔记,
            '问题'=> 探索的问题,
            状态=> '状态',
            作者=> 作者ID,
        ]。
    }
}
 

控制器:

 < PHP

命名空间的应用程序\控制器;

使用Yii的;
使用警予\过滤器\ AccessControl的;
使用警予\网络\控制器;
使用警予\过滤器\ VerbFilter;
使用应用程序\型号\ PromptForm;
使用应用程序\型号\提示符;

类PromptsController扩展控制器
{
    公共职能的actionIndex()
    {
        //返回所有提示的列表:
        返回$这个 - >渲染(索引);
    }

    公共职能actionNew()
    {
        如果(\的Yii :: $ APP->用户自> isGuest){
            返回$这个 - > GOHOME();
        }

        $模式=新PromptForm();
        如果($建模>负载(的Yii :: $ APP->请求 - >后期())){
            如果($提示符= $建模>后期()){
                的Yii :: $ APP->的getSession() - > setFlash('成功','!你的提示创建成功);
                返回$这个 - > GOHOME();
            } 其他 {
                的Yii :: $ APP->的getSession() - > setFlash('错误','错误,同时提交您的提示);
            }
        }

        返回$这个 - >渲染('创造',[
            模式=> $模式,
        ]);
    }
}
 

解决方案

好吧,我理解了它。事实证明,如果你在你的ActiveRecord模型声明公共属性,他们掩盖了由AR创建的自动属性。数据被分配到你的模糊属性,但不会被发送到数据库中。

正确的AR模型应该是简单:

 < PHP

命名空间的应用程序\车型;

使用Yii的;
使用Yii的\ DB \ ActiveRecord的;

一流的提示扩展的ActiveRecord
{
    / **
     *本ActiveRecord类相关联的表@返回字符串的名称。
     * /
    公共静态函数tableName值()
    {
        返回提示;
    }
}
 

I've built a simple form model & view, a simple AR model, and a simple controller. The form model assigns the correct values to the AR instance, but when I call save(), none of those values are saved in the DB. Any ideas?

The form model:

<?php

namespace app\models;

use Yii;
use yii\base\Model;

class PromptForm extends Model
{
    public $name;
    public $intro;
    public $prompt;
    public $notes;
    public $questions;


    public function attributeLabels()
    {
        return [
            'name' => 'Prompt title',
            'intro' => 'Intro',
            'prompt' => 'Prompt body',
            'notes' => 'Closing notes',
            'questions' => 'Exploration questions',
        ];
    }

    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            [['name', 'prompt'], 'required'],
            ['name', 'filter', 'filter' => 'trim'],
            ['name', 'string', 'max' => 255],
            [['intro', 'prompt', 'notes', 'questions'], 'default'],
        ];
    }

    public function post()
    {
        if ($this->validate()) {
            $prompt = new Prompt();
            $prompt->name = $this->name;
            $prompt->intro = $this->intro;
            $prompt->prompt = $this->prompt;
            $prompt->notes = $this->notes;
            $prompt->questions = $this->questions;

            $prompt->author = \Yii::$app->user->getId();

            //die(print_r($prompt, TRUE));

            $prompt->save();

            return $prompt;
        }

        return null;
    }
}

The AR model:

<?php

namespace app\models;

use Yii;
use yii\db\ActiveRecord;

/**
 * Prompt is the model behind the prompt item.
 */
class Prompt extends ActiveRecord
{
    public $name;
    public $intro;
    public $prompt;
    public $notes;
    public $questions;
    public $status;
    public $author;

    public $id;

    /**
     * @return string the name of the table associated with this ActiveRecord class.
     */
    public static function tableName()
    {
        return 'prompt';
    }

    /**
     * @return array the attribute labels.
     */
    public function attributeLabels()
    {
        return [
            'name' => 'Prompt title',
            'intro' => 'Intro',
            'prompt' => 'Prompt body',
            'notes' => 'Closing notes',
            'questions' => 'Exploration questions',
            'status' => 'Status',
            'author' => 'Author ID',
        ];
    }
}

The controller:

<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\PromptForm;
use app\models\Prompt;

class PromptsController extends Controller
{
    public function actionIndex()
    {
        // Return a list of all prompts:
        return $this->render('index');
    }

    public function actionNew()
    {
        if (\Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new PromptForm();
        if ($model->load(Yii::$app->request->post())) {
            if ($prompt = $model->post()) {
                Yii::$app->getSession()->setFlash('success', 'Your prompt was created successfully!');
                return $this->goHome();
            } else {
                Yii::$app->getSession()->setFlash('error', 'Error while submitting your prompt.');
            }
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

解决方案

Okay, I figured it out. Turns out that if you declare public attributes in your ActiveRecord model, they obscure the automatic attributes that are created by AR. Data gets assigned to your obscuring attributes but doesn't get sent into the database.

The correct AR model should have been simply this:

<?php

namespace app\models;

use Yii;
use yii\db\ActiveRecord;

class Prompt extends ActiveRecord
{
    /**
     * @return string the name of the table associated with this ActiveRecord class.
     */
    public static function tableName()
    {
        return 'prompt';
    }
}

这篇关于Yii2活动记录模式不保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆