Php 如何在CodeIgniter 3.1.1中使用Database Forge类
现在我正在使用CodeIgniter 3.1.1开发一个新的应用程序,我需要通过添加新列或删除现有列来自定义数据库列。我已经阅读了CodeIgniter文档,我发现了Database Forge,但是当我试图使用和遵循文档时,我遇到了一些错误!有人能帮我吗Php 如何在CodeIgniter 3.1.1中使用Database Forge类,php,codeigniter,Php,Codeigniter,现在我正在使用CodeIgniter 3.1.1开发一个新的应用程序,我需要通过添加新列或删除现有列来自定义数据库列。我已经阅读了CodeIgniter文档,我发现了Database Forge,但是当我试图使用和遵循文档时,我遇到了一些错误!有人能帮我吗 <?php class Setting extends CI_Controller{ function __construct(){ parent::__construct();
<?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);
}
}
我有它为我的用户_主表,调整你需要的
<?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');
}
}
您似乎没有使用$this->load->Database()加载数据库类
也不是带有$this->load->dbforge()的forge类代码>。加载Forge类的方式($Forge=\Config\Database::Forge();
)就是在Codeigniter 4上加载它的方式。检查锻造厂的Codeigniter 3文档class@JavierLarroulet非常感谢!我读错了文件。我正在读代码点火器4!再次感谢!
<?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');
}
}