Welcome Guest, Not a member yet? Register   Sign In
Database Forge Class does not create the table
#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


Messages In This Thread
RE: Database Forge Class does not create the table - by hc-innov - 03-30-2019, 11:41 AM



Theme © iAndrew 2016 - Forum software by © MyBB