【问题标题】:How to use Database Forge Class in CodeIgniter 3.1.1如何在 CodeIgniter 3.1.1 中使用 Database Forge 类
【发布时间】:2020-12-16 14:03:12
【问题描述】:

现在我正在使用 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

【问题讨论】:

  • 您似乎没有加载带有$this-&gt;load-&gt;database(); 的Database 类,也没有加载带有$this-&gt;load-&gt;dbforge(); 的forge 类。您加载 Forge 类 ($forge = \Config\Database::forge();) 的方式就是您在 Codeigniter 4 上加载它的方式。查看 HERE 以获取有关 Forge 类的 Codeigniter 3 文档
  • @JavierLarroulet 非常感谢!我读过错误的文档。我正在阅读 CodeIgniter 4 !再次感谢!

标签: php codeigniter


【解决方案1】:

我有它用于我的 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');
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-26
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 2022-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多