php - 如何在 CodeIgniter 3.1.1 中使用 Database Forge 类
问题描述
现在我正在使用 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');
}
}
推荐阅读
- python - 来自 scipy 的 RGB 图像卷积
- binance - 有效地从币安 api 获取我的所有交易
- c# - ASP.NET Core 路由空参数
- java - JavaFX 程序在以 JAR 格式启动时停止正常工作
- c# - 即使在 asp.net core 2.2 中使用 PasswordSignInAsync 成功登录后,User.Identity.IsAuthenticated 也总是返回 false
- php - Docker + Xdebug + VSCode 无法连接到客户端
- php - 如何在 mysqli_stmt_bind_param() 方法中使用比参数更多的变量来修复错误
- java - 如何从shell脚本将参数传递给maven
- angular - 如何一一启用 ListView 项的按钮?
- android - 在 Android 4 上启用带有 sha256/sha384 的密码套件