doutui2883
2012-11-29 14:25
采纳率: 100%
浏览 40
已采纳

Kohana ORM - 表名不正确

I have a weird problem with the Kohana (3.2) ORM query builder and i can't figure out what is wrong. I get "Incorrect table name" exception:

  Database_Exception [ 1103 ]: Incorrect table name '' [ SELECT ``.* FROM `` JOIN `user_plugins` ON (`user_plugins`.`plugin_id` = ``.`id`) WHERE `user_plugins`.`user_id` = '9' ]

As you can see the table is empty in the query.

Controller:

  $user = ORM::factory('user', Auth::instance()->get_user()->id);

  if ($user->loaded() ) 
  {
     $result = $user->plugin->find_all();
  }

User model:

  class Model_User extends Useradmin_Model_User
  {
    protected $_has_many = array(
      'plugin' => array( 'through' => 'user_plugins'),
    );
  ...

user_plugin Model

  class Model_user_plugin extends ORM
  {
     protected $_belongs_to = array(
         'plugin' => array(),
         'user' => array()
     );
  ...

plugin Model

  class Model_Plugin extends ORM
  {
     protected $_has_many = array(
         'user' => array('through' => 'user_plugins')
     );
  ...

Anyone got any idea what could be wrong here? Any help is very appreciated!

图片转代码服务由CSDN问答提供 功能建议

我对Kohana(3.2)ORM查询构建器有一个奇怪的问题,我无法弄清楚出了什么问题 。 我得到“不正确的表名”异常:

  Database_Exception [1103]:表名不正确''[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(); 
  } 
   
 
 

用户模型:

 类Model_User扩展Useradmin_Model_User 
 {
 protected $ _has_many = array(  
'plugin'=> array('through'=>'user_plugins'),
); 
 ... 
   
 
 

user_plugin Model < / p>

 类Model_user_plugin扩展ORM 
 {
 protected $ _belongs_to = array(
'plugin'=&gt; array(),
'user'=&gt; array(  )
); 
 ... 
   
 
 

插件模型

  clas  s Model_Plugin扩展ORM 
 {
 protected $ _has_many = array(
'user'=&gt;  array('through'=&gt;'user_plugins')
); 
 ... 
   
 
 

任何人都知道这里有什么问题? Any 非常感谢帮助!

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dtbi27903 2012-12-01 10:57
    已采纳

    This is how User Model should be

    class Model_User extends Useradmin_Model_User
    {
       protected $_has_many = array(
          'plugin' => array('model' => 'plugin', 'through' => 'user_plugins'),
       );
    ...
    

    This is how Plugin Model should be

    class Model_Plugin extends ORM
    {
        protected $_has_many = array(
            'user' => array('model' => 'user', 'through' => 'user_plugins')
        );
    

    You don't need user_plugin Model at all, the "user_plugins" in both models refers to the table name, not the model name. Just make sure you have the table with user_plugins that have following fields,

    id, user_id, plugin_id

    I hope this helps.

    已采纳该答案
    打赏 评论
  • dongqun5769 2012-12-01 11:04

    The $_has_many, by convention, must always use plural names, unless you specify the name in $_object_name in the target model. So it should be:

    class Model_Plugin extends ORM
    {
      protected $_has_many = array(
         'users' => array('through' => 'users_plugins')
      );
      //...
    
    class Model_User extends ORM
    {
      protected $_has_many = array(
         'plugins' => array('through' => 'users_plugins')
      );
      //...
    
    class Model_user_plugin extends ORM
    {
      protected $_belongs_to = array(
         'plugin' => array(),
         'user' => array()
      );
      //...
    
    打赏 评论

相关推荐 更多相似问题