Welcome Guest, Not a member yet? Register   Sign In
Database Forge Class does not create the table
#1

Hi all,
I try to create table into my db but i can't. System give no error messages. Any suggestion? Smile


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

class 
Tables extends CI_Controller {
 
       public function __construct()
 
       {
 
               parent::__construct();
 
               $this->load->model('table_model');
 
       }
 
       public function index()
 
       {
 
               $fields = array(
 
                   'blog_id' => array(
 
                       'type' => 'INT',
 
                       'constraint' => 5,
 
                       'unsigned' => TRUE,
 
                       'auto_increment' => TRUE
                    
),
 
                   'blog_title' => array(
 
                           'type' => 'VARCHAR',
 
                           'constraint' => '100',
 
                           'unique' => TRUE,
 
                   ),
 
                   'blog_author' => array(
 
                           'type' =>'VARCHAR',
 
                           'constraint' => '100',
 
                           'default' => 'King of Town',
 
                   ),
 
                   'blog_description' => array(
 
                           'type' => 'TEXT',
 
                           'null' => TRUE,
 
                   ),
 
               );
 
               $this->table_model->create_table($fields);   
        
}


MODEL

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');
class 
Table_model extends CI_Model {

 
   public function __construct()
 
   {
 
       $this->load->database();
 
       $this->load->dbforge();
 
   }
 
   public function create_table($data)
 
   {
 
       //print_r($data);
 
       $this->dbforge->add_field($data);
 
       $this->dbforge->create_table('mytable',TRUE);
 
   }

Reply
#2

Hi,

you must add a key (I put fields in model, i think it's better).
Your model:
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');
class 
Table_model extends CI_Model {

 
   public function __construct()
 
   {
 
       $this->load->database();
 
       $this->load->dbforge();
 
   }
 
   public function create_table()
 
   {
 
       $fields = array(
 
       'blog_id' => array(
 
               'type' => 'INT',
 
               'constraint' => 5,
 
               'unsigned' => TRUE,
 
               'auto_increment' => TRUE
        
),
 
       'blog_title' => array(
 
               'type' => 'VARCHAR',
 
               'constraint' => '100',
 
               'unique' => TRUE,
 
       ),
 
       'blog_author' => array(
 
               'type' =>'VARCHAR',
 
               'constraint' => '100',
 
               'default' => 'King of Town',
 
       ),
 
       'blog_description' => array(
 
               'type' => 'TEXT',
 
               'null' => TRUE,
 
       ),
 
       );
 
       $this->dbforge->add_field($fields);
 
       $this->dbforge->add_key('blog_id',true);
 
       return $this->dbforge->create_table('mytable',TRUE);
 
   }


The controller:

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

class 
Welcome extends CI_Controller {

    public function 
index()
    {
 
           $this->load->model('Table_model');
 
           $create $this->Table_model->create_table();
 
           if ($create) {
 
               echo 'Table created';
 
           } else {
 
               echo 'Failed to create table';
 
           }
    }

Reply
#3

You can do it using a sql.

PHP Code:
$sql "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)"
;

$result $this->_customQuery($sql);

// -------------------------------------------------------------------

/**
 * _customQuery()
 * -------------------------------------------------------------------
 *
 * @param  $mysqlQuery
 * @return object
 */
public function _customQuery($mysqlQuery)
{
 
   $data = [];

 
   $query $this->db->query($mysqlQuery);

 
   if ($query->num_rows() > 0)
 
   {
 
       $data $query->result();
 
   }

 
   $query->free_result();

 
   return $data;


Place the _customQuery() method in a MY_Model and extend it.

You can do any sql query like that.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB