簡體   English   中英

如何在 CodeIgniter 3.1.1 中使用 Database Forge 類

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

現在我正在使用 CodeIgniter 3.1.1 開發一個新應用程序,我需要通過添加新列或刪除現有列來自定義數據庫列。 我已經閱讀了 CodeIgniter 文檔,我找到了 Database Forge,但是當我嘗試使用並遵循文檔時,我遇到了一些錯誤! 誰能幫我 ?

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

錯誤信息 :

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

我有我的 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