CodeIgniter Forums
DBForge - Create table with Engine MyISAM - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: DBForge - Create table with Engine MyISAM (/showthread.php?tid=43840)



DBForge - Create table with Engine MyISAM - El Forum - 07-25-2011

[eluser]esthezia[/eluser]
Hello there!

How can I create a table with DBForge with MyISAM Engine? I wish to specifically create it with this engine and not letting codeigniter make it with what engine it wants.

I'm using CI 2.0.2.

The first time I created a table it made it MyISAM, but now I see that it automatically creates it InnoDB.

Please help!

Thank you!


DBForge - Create table with Engine MyISAM - El Forum - 07-25-2011

[eluser]InsiteFX[/eluser]
Code:
// --------------------------------------------------------------------

/**
* create_db_table()
*
* Create a new database table using $table_name.
*
* @access    public
* @return    void
*/
public function create_db_table($table_name)
{
    $sql = "CREATE TABLE $table_name (
        `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
        `field_name` varchar(30) NOT NULL,
        PRIMARY KEY (`id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci";

    $result = $this->db->query($sql);
}



DBForge - Create table with Engine MyISAM - El Forum - 07-26-2011

[eluser]esthezia[/eluser]
thank you for your reply!

is this the only way? i was hoping for something using the built-in DBForge functions.


DBForge - Create table with Engine MyISAM - El Forum - 07-26-2011

[eluser]InsiteFX[/eluser]
Yes, because DBForge doe's not have a way to create a new table. I wrote that for someone else that was having the same problem.


DBForge - Create table with Engine MyISAM - El Forum - 07-27-2011

[eluser]esthezia[/eluser]
[quote author="InsiteFX" date="1311734219"]Yes, because DBForge doe's not have a way to create a new table.[/quote]

I believe it does. See here. But it seems that the engine cannot be specified.

Thank you!


DBForge - Create table with Engine MyISAM - El Forum - 07-27-2011

[eluser]InsiteFX[/eluser]
Yes but it defaults to the Innodb not MyISAM which is what he wants to use.


DBForge - Create table with Engine MyISAM - El Forum - 09-12-2012

[eluser]yacman[/eluser]
The least path of resistance, if you wish to have MYISAM exclusively is to overload the CI_DB_Forge_mysql class. To do this you will need to create a MY_Loader.php class file located in your application/core folder.

This is from the sparks generated loader class, abreviated with the additional dbforge overload.
Code:
<?php  if (! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Sparks
*
* An open source application development framework for PHP 5.1.6 or newer
*
* @package  CodeIgniter
* @author  CodeIgniter Reactor Dev Team
* @author      Kenny Katzgrau <[email protected]>
* @since  CodeIgniter Version 1.0
* @filesource
*/

/**
* Loader Class
*
* Loads views and files
*
* @package  CodeIgniter
* @subpackage Libraries
* @author  CodeIgniter Reactor Dev Team
* @author      Kenny Katzgrau <[email protected]>
* @category Loader
* @link  http://ellislab.com/codeigniter/user-guide/libraries/loader.html
*/
class MY_Loader extends CI_Loader
{
   ...

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

/**
  * Load the Database Forge Class
  *
  * @return string
  */
public function dbforge()
{
  if ( ! class_exists('CI_DB'))
  {
   $this->database();
  }

  $CI =& get_instance();

  require_once(BASEPATH.'database/DB_forge.php');
  require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_forge.php');
  /* Look for overload files in the /application/core folder */
  if (file_exists(BASEPATH.'../'.APPPATH.'core/MY_CI_DB_'.$CI->db->dbdriver.'_forge.php')) {
   require_once(BASEPATH.'../'.APPPATH.'core/MY_CI_DB_'.$CI->db->dbdriver.'_forge.php');
   $class = 'MY_CI_DB_'.$CI->db->dbdriver.'_forge';
  } else {  
   $class = 'CI_DB_'.$CI->db->dbdriver.'_forge';
  }

  $CI->dbforge = new $class();
}
}

Next Create a file in your application/libraries folder called:
MY_CI_DB_mysql_forge.php
Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* MySQL Forge Class Force MYISAM
*
* @category Database
* @author
* @link
*/
class MY_CI_DB_mysql_forge extends CI_DB_mysql_forge {
// --------------------------------------------------------------------


/**
  * Create Table
  *
  * @access private
  * @param string the table name
  * @param mixed the fields
  * @param mixed primary key(s)
  * @param mixed key(s)
  * @param boolean should 'IF NOT EXISTS' be added to the SQL
  * @return bool
  */
function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
{

  $sql = 'CREATE TABLE ';

  if ($if_not_exists === TRUE)
  {
   $sql .= 'IF NOT EXISTS ';
  }

  $sql .= $this->db->_escape_identifiers($table)." (";

  $sql .= $this->_process_fields($fields);

  if (count($primary_keys) > 0)
  {
   $key_name = $this->db->_protect_identifiers(implode('_', $primary_keys));
   $primary_keys = $this->db->_protect_identifiers($primary_keys);
   $sql .= ",\n\tPRIMARY KEY ".$key_name." (" . implode(', ', $primary_keys) . ")";
  }

  if (is_array($keys) && count($keys) > 0)
  {
   foreach ($keys as $key)
   {
    if (is_array($key))
    {
     $key_name = $this->db->_protect_identifiers(implode('_', $key));
     $key = $this->db->_protect_identifiers($key);
    }
    else
    {
     $key_name = $this->db->_protect_identifiers($key);
     $key = array($key_name);
    }

    $sql .= ",\n\tKEY {$key_name} (" . implode(', ', $key) . ")";
   }
  }

  $sql .= "\n) ENGINE=MYISAM DEFAULT CHARACTER SET {$this->db->char_set} COLLATE {$this->db->dbcollat};";

  return $sql;
}
}

Adding these will enable the $this->dbforge->create_table() method to return the sql for a MYISAM table.

If you have access to the mysql instance, you could force the default table engine to be MYISAM or whatever using the --default-storage-engine or --default-table-type at startup.

This is a pretty good write up I found as well for older versions of CI


DBForge - Create table with Engine MyISAM - El Forum - 02-09-2013

[eluser]Kaabi[/eluser]
You can put this line after dbforge create_table

Code:
$this->db->query('ALTER TABLE  `table_name` ENGINE = MYISAM');



DBForge - Create table with Engine MyISAM - El Forum - 12-28-2013

[eluser]Unknown[/eluser]
Set default engine to MYISAM before creating tables:

Code:
$this->db->query('SET storage_engine=MYISAM;');

Reference: http://dev.mysql.com/doc/refman/5.7/en/storage-engine-setting.html