CodeIgniter Forums
Creating Tables - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Creating Tables (/showthread.php?tid=68005)



Creating Tables - robertnicjoo - 05-08-2017

Hello,

I use to work with Laravel and I want to know if there is any way such as migrantion in laravel for Codeigniter I mean to create tables and roles of them then just run it in command and it creates tables automatically so no need to work on my phpmyadmin and input many lines of codes to create tables, is there?

Thanks.


RE: Creating Tables - qury - 05-09-2017

(05-08-2017, 04:43 PM)robertnicjoo Wrote: Hello,

I use to work with Laravel and I want to know if there is any way such as migrantion in laravel for Codeigniter I mean to create tables and roles of them then just run it in command and it creates tables automatically so no need to work on my phpmyadmin and input many lines of codes to create tables, is there?

Thanks.


Hi, there is an ugly way to do something like this:

PHP Code:
$source $this->load->database('source_db'TRUE);
$target $this->load->database('target_db'TRUE);

$this->myforge $this->load->dbforge($targettrue);

 
$fields $source->field_data($table);
 
           
                    $fields2 
= array();
 
                   foreach ($fields as $fld)
 
                   {
 
                       $fields2[$fld->name        = (array) $fld;
 
                       $fields2[$fld->name]['null'] = true;

 
                       unset($fields2[$fld->name]['name']);

 
                       if ($fields2[$fld->name]['default'])
 
                       {
 
                           $fields2[$fld->name]['default'] = (string) $fields2[$fld->name]['default'];
 
                       } else
 
                       {
 
                           unset($fields2[$fld->name]['default']);
 
                       }

 
                       if ($fields2[$fld->name]['max_length'])
 
                       {
 
                           $fields2[$fld->name]['constraint'] = (int) $fields2[$fld->name]['max_length'];
 
                           unset($fields2[$fld->name]['max_length']);
 
                       }

 
                       unset($fields2[$fld->name]['primary_key']);
 
                   }

 
                   $this->myforge->add_field($fields2);
 
                   $this->myforge->create_table($tableTRUE);                 



RE: Creating Tables - marksman - 05-09-2017

I think you have to read the docs first. CodeIgniter has a very nice documentation.

https://www.codeigniter.com/user_guide/libraries/migration.html

its also available in your local copy under user_guide