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!

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)