简体   繁体   English

如何在 CodeIgniter 3.1.1 中使用 Database Forge 类

[英]How to use Database Forge Class in CodeIgniter 3.1.1

Now I am developing a new app using CodeIgniter 3.1.1, I need to custom the database column by adding a new column or delete the existing column.现在我正在使用 CodeIgniter 3.1.1 开发一个新应用程序,我需要通过添加新列或删除现有列来自定义数据库列。 I have read the CodeIgniter documentation, I found Database Forge, but when i'm trying to use and follow the documentation, i got some error !我已经阅读了 CodeIgniter 文档,我找到了 Database Forge,但是当我尝试使用并遵循文档时,我遇到了一些错误! can anyone help me ?谁能帮我 ?

<?php 

class Setting extends CI_Controller{
    function __construct(){
        parent::__construct();      
        $this->load->model('m_stock');
        if($this->session->userdata('status') != "login"){
            redirect(base_url("login"));
        }
        $forge = \Config\Database::forge();
    }

    function index(){

    }

    function tambah_cabang(){
        $fields = [
            'cabang_3' => ['type' => 'TEXT']
        ];
        $forge->addColumn('barang', $fields);
    }
}

Error Message :错误信息 :

An uncaught Exception was encountered

Type: Error

Message: Class 'Config\Database' not found

Filename: E:\xampp\htdocs\belajarphp\pos\application\controllers\Setting.php

Line Number: 10

Backtrace:

File: E:\xampp\htdocs\belajarphp\pos\index.php
Line: 315
Function: require_once

I have it for my user_master table, tweak as you need我有我的 user_master 表,根据需要调整

<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class UserMaster extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type' => 'INT',
                'auto_increment' => TRUE
            ],
            'name' => [
                'type' => 'VARCHAR',
                'constraint' => '50',
            ],
            'username' => [
                'type' => 'VARCHAR',
                'constraint' => '50',
            ],
            'password' => [
                'type' => 'VARCHAR',
                'constraint' => '50',
            ],
            'access_level' => [
                'type' => 'VARCHAR',
                'constraint' => '50',
            ],
        ]);
        $this->forge->addField("date_time timestamp DEFAULT CURRENT_TIMESTAMP");
        $this->forge->addPrimaryKey('id', TRUE);
        $this->forge->createTable('user_master');
    }

    //--------------------------------------------------------------------

    public function down()
    {
        $this->forge->dropTable('user_master');
    }
}

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

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