Php Kohana ORM-表名不正确
我对Kohana(3.2)ORM查询生成器有一个奇怪的问题,我不知道是什么错了。我收到“表名不正确”异常:Php Kohana ORM-表名不正确,php,kohana,kohana-3,kohana-orm,kohana-3.2,Php,Kohana,Kohana 3,Kohana Orm,Kohana 3.2,我对Kohana(3.2)ORM查询生成器有一个奇怪的问题,我不知道是什么错了。我收到“表名不正确”异常: Database_Exception [ 1103 ]: Incorrect table name '' [ SELECT ``.* FROM `` JOIN `user_plugins` ON (`user_plugins`.`plugin_id` = ``.`id`) WHERE `user_plugins`.`user_id` = '9' ] 如您所见,查询中的表为空 控制器:
Database_Exception [ 1103 ]: Incorrect table name '' [ SELECT ``.* FROM `` JOIN `user_plugins` ON (`user_plugins`.`plugin_id` = ``.`id`) WHERE `user_plugins`.`user_id` = '9' ]
如您所见,查询中的表为空
控制器:
$user = ORM::factory('user', Auth::instance()->get_user()->id);
if ($user->loaded() )
{
$result = $user->plugin->find_all();
}
用户模型:
class Model_User extends Useradmin_Model_User
{
protected $_has_many = array(
'plugin' => array( 'through' => 'user_plugins'),
);
...
用户插件模型
class Model_user_plugin extends ORM
{
protected $_belongs_to = array(
'plugin' => array(),
'user' => array()
);
...
class Model_Plugin extends ORM
{
protected $_has_many = array(
'user' => array('through' => 'user_plugins')
);
...
插件模型
class Model_user_plugin extends ORM
{
protected $_belongs_to = array(
'plugin' => array(),
'user' => array()
);
...
class Model_Plugin extends ORM
{
protected $_has_many = array(
'user' => array('through' => 'user_plugins')
);
...
有人知道这里可能出了什么问题吗?
非常感谢您的帮助 用户模型应该是这样的
class Model_User extends Useradmin_Model_User
{
protected $_has_many = array(
'plugin' => array('model' => 'plugin', 'through' => 'user_plugins'),
);
...
class Model_Plugin extends ORM
{
protected $_has_many = array(
'user' => array('model' => 'user', 'through' => 'user_plugins')
);
插件模型应该是这样的
class Model_User extends Useradmin_Model_User
{
protected $_has_many = array(
'plugin' => array('model' => 'plugin', 'through' => 'user_plugins'),
);
...
class Model_Plugin extends ORM
{
protected $_has_many = array(
'user' => array('model' => 'user', 'through' => 'user_plugins')
);
您根本不需要用户插件模型,两个模型中的“用户插件”都是指表名,而不是模型名。只需确保您的表中包含具有以下字段的user_插件
id,用户id,插件id
我希望这能有所帮助。这就是用户模型应该是什么样子
class Model_User extends Useradmin_Model_User
{
protected $_has_many = array(
'plugin' => array('model' => 'plugin', 'through' => 'user_plugins'),
);
...
class Model_Plugin extends ORM
{
protected $_has_many = array(
'user' => array('model' => 'user', 'through' => 'user_plugins')
);
插件模型应该是这样的
class Model_User extends Useradmin_Model_User
{
protected $_has_many = array(
'plugin' => array('model' => 'plugin', 'through' => 'user_plugins'),
);
...
class Model_Plugin extends ORM
{
protected $_has_many = array(
'user' => array('model' => 'user', 'through' => 'user_plugins')
);
您根本不需要用户插件模型,两个模型中的“用户插件”都是指表名,而不是模型名。只需确保您的表中包含具有以下字段的user_插件
id,用户id,插件id
我希望这会有所帮助。根据惯例,
$\u有许多
,必须始终使用复数名称,除非您在目标模型的$\u object\u name
中指定名称。因此,它应该是:
类模型插件扩展了ORM
{
受保护的$\u有\u多个=数组(
'users'=>array('通过'=>'users\u插件')
);
//...
类模型\u用户扩展ORM
{
受保护的$\u有\u多个=数组(
'plugins'=>array('through'=>'users\u plugins')
);
//...
类模型用户插件扩展ORM
{
受保护的$属于=数组(
'plugin'=>array(),
'user'=>array()
);
//...
根据惯例,$\u有许多
,必须始终使用复数名称,除非您在目标模型中的$\u object\u name
中指定名称。因此应为:
类模型插件扩展了ORM
{
受保护的$\u有\u多个=数组(
'users'=>array('通过'=>'users\u插件')
);
//...
类模型\u用户扩展ORM
{
受保护的$\u有\u多个=数组(
'plugins'=>array('through'=>'users\u plugins')
);
//...
类模型用户插件扩展ORM
{
受保护的$属于=数组(
'plugin'=>array(),
'user'=>array()
);
//...
Useradmin\u Model\u User
?PS中的内容。为什么对关系名称使用单数形式?应该是users
、plugins
等。感谢您的回复。我正在使用Mixu Useradmin模块,并进行一些小的修复(主要是为了使其与Kohana 3.2兼容)。你可以在这里找到源代码:你能给我看一下var\u dump($user)
?嗯,这很奇怪。我跟踪了这个问题到Kohana\u ORM类中的uu get函数。当$model=ORM::factory($this->\u有许多[$column]['model'])时,
行设置了相关的模型(在Kohana\u ORM的第580行附近)“\u object\u name”变量设置不正确,随后将用于创建第二个联接列$join\u col2=$model->\u object\u name...$model->\u primary\u key;
知道为什么会失败吗?这是$user的转储:这是创建的$model对象的转储:可能您更改了插件的\u initialize()
method?在Model\u Plugin
code中有什么?在Useradmin\u Model\u User
中有什么?PS.为什么要使用单数形式的关系名称?应该是users
,plugins
等等。感谢您的回复。我正在使用Mixu Useradmin模块,并做了一些小的修改(主要是为了使它与Kohana 3.2兼容)。你可以在这里找到源代码:你能给我看一下var\u dump($user)
?嗯,这很奇怪。我跟踪了这个问题到Kohana\u ORM类中的uu get函数。当$model=ORM::factory($this->\u有许多[$column]['model'])时,
行设置了相关的模型(在Kohana\u ORM的第580行附近)“\u object\u name”变量设置不正确,随后将用于创建第二个联接列$join\u col2=$model->\u object\u name...$model->\u primary\u key;
知道为什么会失败吗?这是$user的转储:这是创建的$model对象的转储:可能您更改了插件的\u initialize()
method?Model\u Plugin中的内容是什么?
code?我对这个插件的植入做了一些更改,并且知道即使没有设置$\u object\u名称,它也能工作。我不是100%确定这是什么原因造成的,但现在它工作了,所以我很高兴:)我对这个插件的植入做了一些更改,并且知道即使没有设置$\u object\u名称,它也能工作。我不是100%su是什么导致了这一切,但现在它起作用了,所以我很高兴:)