加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

activerecord – Yii2活动记录模式不保存数据

发布时间:2020-12-13 14:15:41 所属栏目:PHP教程 来源:网络整理
导读:我已经建立了一个简单的模型查看,一个简单的AR模型和一个简单的控制器.表单模型将正确的值分配给AR实例,但是当我调用save()时,这些值都不会保存在DB中.有任何想法吗? 表单模型: ?phpnamespace appmodels;use Yii;use yiibaseModel;class PromptForm ext
我已经建立了一个简单的模型&查看,一个简单的AR模型和一个简单的控制器.表单模型将正确的值分配给AR实例,但是当我调用save()时,这些值都不会保存在DB中.有任何想法吗?

表单模型:

<?php

namespace appmodels;

use Yii;
use yiibaseModel;

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'],'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;
    }
}

AR模型:

<?php

namespace appmodels;

use Yii;
use yiidbActiveRecord;

/**
 * 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','status' => 'Status','author' => 'Author ID',];
    }
}

控制器:

<?php

namespace appcontrollers;

use Yii;
use yiifiltersAccessControl;
use yiiwebController;
use yiifiltersVerbFilter;
use appmodelsPromptForm;
use appmodelsPrompt;

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,]);
    }
}
好的,我想出来了原来,如果您在ActiveRecord模型中声明公共属性,它们会掩盖由AR创建的自动属性.数据被分配给您的模糊属性,但不会发送到数据库.

正确的AR模型应该是这样的:

<?php

namespace appmodels;

use Yii;
use yiidbActiveRecord;

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读