简体   繁体   English

yii向数据库添加数据时出错

[英]yii error on adding data to DB

include(label.php): failed to open stream: No such file or directory
(/Applications/AMPPS/www/framework-1.1.17/YiiBase.php:432)

I am getting the above error on yii1 storing data. 我在yii1存储数据时遇到上述错误。 Below is my model file and controller function to add data to DB. 下面是我的模型文件和控制器功能,用于向DB添加数据。 Am I missing something in this? 我在其中缺少什么吗?

<?php
 class account extends CoreModel {

public function tableName() {
    return 'account';
}

public function rules() {
    return array(
        array('id', 'label', 'created_at', 'updated_at', 'required'),
        array('id', 'integer'),
        array('created_at', 'updated_at', 'safe'),
        array('label', 'string', 'max' => 20),
    );
}
public function attributeLabels() {
    return array(
        'id' => 'ID',
        'label' => 'Label',
        'created_at' => 'Created At',
        'updated_at' => 'Updated At',
    );
}
 }

Controller function: 控制器功能:

function saveDetails(){
    $abc = new Account();

    $abc->id = Yii::app()->user->id;
    $abc->label = "Test";
    $abc->save();
    echo "done";
    exit;
}

Change this: 更改此:

array('id', 'label', 'created_at', 'updated_at', 'required'),

to this: 对此:

array(array('id', 'label', 'created_at', 'updated_at'), 'required'),

Same here: 同样在这里:

array('created_at', 'updated_at', 'safe'),

to: 至:

array(array('created_at', 'updated_at'), 'safe'),

But you dont need to specify created_at and updated_at as safe , because they occurs in required rule. 但是您不需要将created_atupdated_at指定为safe ,因为它们出现在required规则中。

Seems like your $abc->label is triggering an autoload request for some reason. 似乎您的$abc->label由于某种原因触发了自动加载请求。 Probably due to a conflicting library. 可能是由于库冲突。 You could use CActiveRecord 's setAttributes() and rewrite like this to eliminate magic method/property calls and autoload confusions. 您可以使用CActiveRecordsetAttributes()并像这样重写,以消除魔术方法/属性调用和自动加载混乱。

Controller function 控制器功能

function saveDetails(){
    $abc = new Account();
    $abc->setAttributes([
        'id' => Yii::app()->user->id,
        'label' => "Test"
    ]);
    $abc->save();
    echo "done";
    //exit;
    Yii::app()->end(); // This is the clean exit from a Yii app.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM