Where should I initialize the Forge class in CodeIgniter and use the create_database function?

I am reading this documentation regarding the Forge CodeIgniter class and I love the idea that I can create a new DB using this class, this is just what I need. I need to use this create_database

$this->dbforge->create_database('db_name')

      

But I was wondering where the Forge Class code should look like ? It must have a better place in one of the controllers? Where is the best practice for this? should it be in the library or in the transfer folder, if so, in which function? function up ? CodeIgniter is not listed in the documentation!

this is what I have so far added the code to create the table, but where the create_database function works :

<?php

class Migration_Users extends CI_Migration {
    public function up(){
        $this->dbforge->add_field("`id` int(11) NOT NULL AUTO_INCREMENT");
        $this->dbforge->add_field("`role_id` int(11) NOT NULL DEFAULT '2'");
        $this->dbforge->add_field("`username` varchar(25) COLLATE utf8_bin NOT NULL UNIQUE");
        $this->dbforge->add_field("`password` varchar(1024) COLLATE utf8_bin NOT NULL");
        $this->dbforge->add_field("`email` varchar(100) COLLATE utf8_bin NOT NULL UNIQUE");
        $this->dbforge->add_field("`banned` tinyint(1) NOT NULL DEFAULT '0'");
        $this->dbforge->add_field("`ban_reason` varchar(255) COLLATE utf8_bin DEFAULT NULL");
        $this->dbforge->add_field("`newpass` varchar(34) COLLATE utf8_bin DEFAULT NULL");
        $this->dbforge->add_field("`newpass_key` varchar(32) COLLATE utf8_bin DEFAULT NULL");
        $this->dbforge->add_field("`newpass_time` datetime DEFAULT NULL");
        $this->dbforge->add_field("`last_ip` varchar(40) COLLATE utf8_bin NOT NULL");
        $this->dbforge->add_field("`last_login` datetime NOT NULL DEFAULT '0000-00-00 00:00:00'");
        $this->dbforge->add_field("`created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00'");
        $this->dbforge->add_field("`modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP");
        $this->dbforge->add_field("`deleted` tinyint(1) NOT NULL DEFAULT '0'");

        $this->dbforge->add_key('id', TRUE);

        $this->dbforge->create_table('users', TRUE);
    }

    public function down(){
        $this->dbforge->drop_table('users');
    }
}

      

+3


source to share


1 answer


All your web need try google :)

You need to create a file inside this new directory, and the naming format should start with your version number followed by your class name (make a description of the descriptions of the changes you are making, i.e. your first could be: initial_schema). My first migration file was called: 001_initial_schema.php and my next one might be 002_add_comments. Your file will look something like this:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Migration_Initial_schema extends CI_Migration {

    public function up()
    {
        $this->dbforge->add_field(array(
            'postid' => array(
                'type' => 'INT',
                'constraint' => 11,
                'unsigned' => TRUE,
                'auto_increment' => TRUE
            ),
            'title' => array(
                'type' => 'VARCHAR',
                'constraint' => '100',
            ),
            'content' => array(
                'type' => 'TEXT',
                'null' => TRUE,
            ),
        ));

        $this->dbforge->create_table('posts');
    }

    public function down()
    {
        $this->dbforge->drop_table('posts');
    }
}

      

You will see that there are two functions in this class (up and down). The up function is triggered when upgrading to / after this version, and also the down function is triggered if you downgrade your database (using a config file) below this version. Your down function will always make a back reference to what your up function is doing. Essentially restoring the database to the state it was before this version. Also note that the class name above matches the suggested filename I gave earlier (except for the word Migration), the code expects the class to be specified this way and won't work without it.



I won't go into much detail on the format that will be used for the array of fields when creating or modifying tables, as the CodeIgniters user guide in the Forge database class already explains this perfectly.

The only warning I noticed is when you want to have a textbox or varchar field with a default null value. Usually in phpMyAdmin you should check the field Null and also set the default to Null. However, all you have to do is specify null as TRUE, which you can see from my example above. If you try to define the default as null, it will actually set the default to an empty string. I suppose this problem will be sorted out eventually, but as long as you remember it, it is not difficult to avoid.

Insert in Pra

0


source







All Articles