首页 \ 问答 \ Codeigniter 3 - 如何使用Forge Class创建数据库模式?(Codeigniter 3 - How to use Forge Class to create a database schema?)

Codeigniter 3 - 如何使用Forge Class创建数据库模式?(Codeigniter 3 - How to use Forge Class to create a database schema?)

我使用CodeIgniter 3.1.0来开发一个应用程序。 为了改进安装,我写了一个Install_Controller和一个Install_Model。

我想使用Database Forge类来管理数据库模式,但在用户指南中不是很清楚,Google上也没有提供帮助。

我需要使用$ this-> dbforge-> create_database ,因为用户对数据库一无所知,所以他会做的只是MySQL“接下来,安装”,然后运行一个运行PHP作为Web服务器的批处理文件,所以从Chrome他可以使用URL来安装应用程序。

用户指南说: 为了初始化Forge类,您的数据库驱动程序必须已经运行,因为伪造类依赖它。

所以我已经用user,pwd,dbname等设置了config / database.php ...甚至因为我需要它在app中使用。

当我尝试加载URL来安装应用程序时,给我错误: 消息:mysqli :: real_connect():(HY000 / 1049):未知数据库'测试'

那么,如果我需要首先创建数据库模式,我该如何使用Forge Class来创建数据库模式呢?

一些代码...

$db['default'] = array(
    'dsn' => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => 'root',
    'database' => 'test',
    'dbdriver' => 'mysqli'
);

$autoload['libraries'] = array('database');

class Install extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->load->model('install_model');
        $this->install_model->createDabase();
    }
}

class Install_model extends CI_Model {
    function __construct() {
        parent::__construct();
    }

    function createDabase() {
        $this->load->dbforge();

        if ($this->dbforge->create_database('test'))
            echo 'Database created!';
        else
            echo 'Database error!';
    }
}

I'm using CodeIgniter 3.1.0 to develop an app. In order to improve installation, I've written an Install_Controller, and an Install_Model.

I want to use Database Forge class to manage the database schema, but it's not very clear in user guide and nothing on Google helps.

I need to use $this->dbforge->create_database, because the user knows nothing about database, so all he will do is MySQL "Next, next, install" and then run a batch file that run PHP as web server, so from Chrome he can use URL to install the app.

User guide says: In order to initialize the Forge class, your database driver must already be running, since the forge class relies on it.

So I have setup the config/database.php with user, pwd, dbname and so on... Even because I need it to use in app.

When I try to load the URL to install the app, give me the error: Message: mysqli::real_connect(): (HY000/1049): Unknown database 'test'

So, how can I use Forge Class to create database schema, if I need to have it first?

Some code...

$db['default'] = array(
    'dsn' => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => 'root',
    'database' => 'test',
    'dbdriver' => 'mysqli'
);

$autoload['libraries'] = array('database');

class Install extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->load->model('install_model');
        $this->install_model->createDabase();
    }
}

class Install_model extends CI_Model {
    function __construct() {
        parent::__construct();
    }

    function createDabase() {
        $this->load->dbforge();

        if ($this->dbforge->create_database('test'))
            echo 'Database created!';
        else
            echo 'Database error!';
    }
}

原文:https://stackoverflow.com/questions/41697121
更新时间:2023-11-10 06:11

最满意答案

这听起来像一个复合主键是你的正确解决方案:

CREATE TABLE emp (
   subsidiary_id  smallint  NOT NULL, 
   employee_id    int       NOT NULL,
   emp_name       text,
   CONSTRAINT emp_pkey PRIMARY KEY (subsidiary_id, employee_id)
);

为此约束创建的索引对于仅在WHERE条件中仅出现subsidiary_id查询也很有用。


It sounds like a composite primary key is the right solution for you:

CREATE TABLE emp (
   subsidiary_id  smallint  NOT NULL, 
   employee_id    int       NOT NULL,
   emp_name       text,
   CONSTRAINT emp_pkey PRIMARY KEY (subsidiary_id, employee_id)
);

The index that is created for this constraint will also be useful for queries where only subsidiary_id appears in the WHERE condition.

相关问答

更多
  • 正确的方式是(不是OP想要的) 你可以使用@JoinColumns注解(注意复数而不是单数),从而加入: @Entity @Table(name = "TABLE_A") @IdClass(TableAPk.class) public class TableA implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name = "COLUMN_1", null ...
  • 你的理解是正确的。 你会在许多情况下这样做。 一个例子就是像OrderHeader和OrderDetail这样的关系。 OrderHeader的PK可能是OrderNumber 。 OrderDetail的PK可能是OrderNumber AND LineNumber 。 如果是两者之一,那就不是独一无二的,而是两者的结合是保证独一无二的。 另一种方法是使用生成(非智能)主键,例如在这种情况下为OrderDetailId 。 但是,你不会总是很容易地看到关系。 一些人喜欢一种方式; 一些人喜欢另一种方式。 ...
  • 这听起来像一个复合主键是你的正确解决方案: CREATE TABLE emp ( subsidiary_id smallint NOT NULL, employee_id int NOT NULL, emp_name text, CONSTRAINT emp_pkey PRIMARY KEY (subsidiary_id, employee_id) ); 为此约束创建的索引对于仅在WHERE条件中仅出现subsidiary_id查询也很有用。 I ...
  • 数据库规范化坚果会告诉你一件事。 我只是想提供我自己多年来学到的东西的意见..我把一个AutoIncrementing ID字段粘贴到Every($&(@#$)#我的一张桌子上,这让我的生活变得更容易一百万倍从长远来看,能够单挑而不受惩罚。 这是来自“在战壕中”的开发者。 Database Normalization nuts will tell you one thing. I'm just going to offer my own opinion of what i've learned over t ...
  • 如果您无法预测有多少(最多)用户与一个企业相关,则无法使用序列(即使可以,我也不会建议使用增量方式)。 如果使用Postgres> = 9,可以使用WITH来避免最大查询,方法是将计数器作为主表的列(或创建一个包含business_id(PK)对的新表,如果不能更改则创建next_id商业表)。 添加列next_id: ALTER TABLE Businesses ADD COLUMN next_id bigint; //update current rows with default value ALTE ...
  • 不幸的是,正如您所推断的那样,您将无法以当前形式映射该关系。 但是,有一个解决方法。 不是将Nephew.Child映射为Nephew.Child ,而是将Nephew.Child映射为常规属性,并在需要检索Child时使用查询。 黑客还有一次机会。 当且仅当Nephew.Child不为null且不可变时,您可以映射引用Child而不是Parent的键:
    可能我添加代理键主键以及唯一复合的主要原因是,如果我需要它来为另一个表的外键。 第二个是如果我怀疑主要复合键在不久的将来可能不会是唯一的。 除此之外,你必须谈论某种非常具体的优化。 最好总是主观的。 Probably the main reason I'd add a surrogate key primary key as well as a unique composite, is if I needed it for a foreign key for another table. The secon ...
  • 您需要将约束放在单个引用中: CONSTRAINT fk_BOX FOREIGN KEY(CLIENT_CODE, BOX_CODE, BOX_NUMBER) REFERENCES BOX(CLIENT_CODE, BOX_CODE, BOX_NUMBER) 但是,我建议您在BOX上放置一个标识int主键,并将其用于外键引用。 您可以在BOX(CLIENT_CODE, BOX_CODE, BOX_NUMBER)上声明UNIQUE约束以防止重复。 You need to put the constr ...
  • 你的问题没有多大意义。 您的主题行要求输入“复合外键”,您的第一句话要求使用您的示例代码忽略的AUTOINCREMENT的“复合主键”。 我将以这种方式解释您的问题:您希望表中的_ID INTEGER PRIMARY KEY AUTOINCREMENT列能够使用Android的CursorAdapter ,但您还要确保其他两列的组合是唯一的。 在这种情况下,我认为你想使用UNIQUE约束: SQLite中的多个唯一列 SQLite表约束 - 在多列上唯一 http://sqlite.org/lang_cre ...
  • 这是我们在编程中遇到的很多情况之一。 Technology_A不会让我做Operation_B,但它应该! 然后,在将我们的头撞到墙上一段时间后,我们放弃(或前往Stackoverflow)。 我们这样做是因为我们没有按照预期使用该技术(这是好的,这就是我们学习的方式!) 这里的问题是您的数据库架构。 你试图用一张桌子做太多。 您不应该在跟踪出现次数的同一个表中存储票证(同一票证可能存在多次)。 在我看来,有两个很好的策略来纠正这种情况。 首先,我将创建一个带有单列主键的票证表。 然后,我将创建一个存储票证 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。