duangonglian6028 2012-12-25 17:10
浏览 14
已采纳

kohana ORM foreign_key

I'm trying to find a way to get data from different related tables in Kohana .

I have the file table which is defined as :

class Model_File extends ORM {

    protected $_belongs_to = array
    (
    'student' => array ('foreign_key' => 'student_id' )
    );
}

Then the session Table :

class Model_Filesession extends ORM {

    protected $_primary_key = 'id';
    protected $_table_name = 'file_sessions';  


    protected $_belongs_to = array
    (
    'file'       => array ('modele'=> 'file'       , 'foreign_key' => 'file_id'     ),
    'subject'    => array ('modele'=> 'subject'    , 'foreign_key' => 'subject_id'  ),
    'place'      => array ('modele'=> 'place'      , 'foreign_key' => 'place_id'    ),
    'teacher'    => array ('modele'=> 'teacher'    , 'foreign_key' => 'teacher_id'  )
    );

}

So there is no direct link between filesession and student ... So i can't add it into the Join of the Filesession (->with('student'))

Currently i'm doing this :

        $fileSessions  =   ORM::factory('filesession')
        ->with('subject')
        ->with('teacher')
        ->with('place')
        ->with('file')
        ->where('payment_id','=',$payment_id)
        ->order_by('sessionDate','DESC')
        ->find_all();

How can I modify this query to JOIN on the student table ?

In another word ... I just need to add the following :

INNER JOIN students ON students.id = file.student_id

But using the Kohana ORM

Edit (Student Model added)

class Model_Student extends ORM {

    protected $_has_one = array(
    'file' => array(
    'model'       => 'file',
    'foreign_key' => 'student_id',
    ),
    );

     protected $_belongs_to = array
    (
    'level' => array ('foreign_key' => 'level_id' )
    );

}
  • 写回答

1条回答 默认 最新

  • douchun1859 2012-12-28 09:06
    关注

    You can use join and on just as you would in the DB query builder

        $fileSessions  =   ORM::factory('filesession')
        ->with('subject')
        ->with('teacher')
        ->with('place')
        ->with('file')
        ->join(array('students','student'))->on('student.id', '=', 'file.student_id')
        ->where('payment_id','=',$payment_id) 
        ->order_by('sessionDate','DESC')
        ->find_all();
    

    or you can use the $_load_with property on the file model. It does the loading automatically for you, so you don't need a second with call.

    class Model_File extends ORM {
    
      protected $_belongs_to = array
      (
      'student' => array ('foreign_key' => 'student_id' )
      );
      protected $_load_with = array('student');
    }
    

    When you load the File model, you can access it by using $file->student automatically, and on your Filesession for example, it would be $filesession->file->student

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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