我正在尝试创建使用迁移的CI插件系统表。以下是迁移文件的代码。
public function up()
{
$fields = array(
'plugin_id bigint(20) unsigned NOT NULL AUTO_INCREMENT',
'plugin_system_name varchar(255) NOT NULL',
'plugin_uri varchar(120) DEFAULT NULL',
'plugin_version varchar(30) NOT NULL',
'plugin_description text',
'plugin_author varchar(120) DEFAULT NULL',
'plugin_author_uri varchar(120) DEFAULT NULL',
'plugin_data longtext'
);
$this->dbforge->add_field($fields);
$this->db->query('UNIQUE KEY `plugin_index` (`plugin_system_name`) USING BTREE');
$this->db->query('ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=latin1');
$this->dbforge->add_key('plugin_id', true);
$this->dbforge->create_table('plugins');
}不,它当然是给了我下面的错误
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UNIQUE KEY `plugin_index` (`plugin_system_name`) USING BTREE' at line 1
UNIQUE KEY `plugin_index` (`plugin_system_name`) USING BTREE
Filename: \xampp\htdocs\myapp\system\database\DB_driver.php
Line Number: 330而CI插件sql如下所示
DROP TABLE IF EXISTS `plugins`;
CREATE TABLE `plugins` (
`plugin_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`plugin_system_name` varchar(255) NOT NULL,
`plugin_name` varchar(255) NOT NULL,
`plugin_uri` varchar(120) DEFAULT NULL,
`plugin_version` varchar(30) NOT NULL,
`plugin_description` text,
`plugin_author` varchar(120) DEFAULT NULL,
`plugin_author_uri` varchar(120) DEFAULT NULL,
`plugin_data` longtext,
PRIMARY KEY (`plugin_id`),
UNIQUE KEY `plugin_index` (`plugin_system_name`) USING BTREE
) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;我已经读过Forge的文档,但它们还没有详细描述有关的所有方面。因此,如果有人能帮助我解决这个问题,并使之与移民局合作,我将不胜感激。
发布于 2013-12-31 13:23:11
试试这个:
$sql = "DROP TABLE IF EXISTS `plugins`;
CREATE TABLE `plugins` (
`plugin_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`plugin_system_name` varchar(255) NOT NULL,
`plugin_name` varchar(255) NOT NULL,
`plugin_uri` varchar(120) DEFAULT NULL,
`plugin_version` varchar(30) NOT NULL,
`plugin_description` text,
`plugin_author` varchar(120) DEFAULT NULL,
`plugin_author_uri` varchar(120) DEFAULT NULL,
`plugin_data` longtext,
PRIMARY KEY (`plugin_id`),
UNIQUE KEY `plugin_index` (`plugin_system_name`) USING BTREE
) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=latin1; ";
$this->db->query($sql);根据这个帖子,这是唯一的方法,因为DBForge没有创建新表的方法。
更新:
我认为这个帖子是一个合适的解决方案。如果链接中断,我会在这里给它:
如果您希望MYISAM只使用MYISAM,抵抗的最小路径是重载CI_DB_Forge_mysql类。为此,您需要创建一个MY_Loader.php类文件>位于您的应用程序/核心文件夹中。 这是由sparks生成的加载器类生成的,它与附加的dbforge >重载一起简略化。
<?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 <katzgrau@gmail.com>
* @since CodeIgniter Version 1.0
* @filesource
*/
/**
* Loader Class
*
* Loads views and files
*
* @package CodeIgniter
* @subpackage Libraries
* @author CodeIgniter Reactor Dev Team
* @author Kenny Katzgrau <katzgrau@gmail.com>
* @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();
}
} 接下来,在应用程序/库文件夹中创建一个名为: MY_CI_DB_mysql_forge.php的文件
<?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;
}
} 添加这些内容将使$this->dbforge>create_ table ()方法能够返回MYISAM表的sql。 如果您可以访问mysql实例,则可以在启动时使用-默认存储引擎或-默认表类型强制默认表引擎为MYISAM或其他任何类型。 这是一个很好的写作,我也发现,对于旧版本的CI。
我希望这能帮到你。
https://stackoverflow.com/questions/20857575
复制相似问题