我正在尝试找到一种从 Kohana 的不同相关表中获取数据的方法。
我有定义为的文件表:
class Model_File extends ORM {
protected $_belongs_to = array
(
'student' => array ('foreign_key' => 'student_id' )
);
}
然后会话表:
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' )
);
}
所以文件会话和学生之间没有直接联系......所以我不能将它添加到文件会话的加入中(->with('student'))
目前我正在这样做:
$fileSessions = ORM::factory('filesession')
->with('subject')
->with('teacher')
->with('place')
->with('file')
->where('payment_id','=',$payment_id)
->order_by('sessionDate','DESC')
->find_all();
如何将此查询修改为 JOIN 在学生表上?
换句话说......我只需要添加以下内容:
INNER JOIN students ON students.id = file.student_id
但是使用 Kohana ORM
编辑(添加了学生模型)
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' )
);
}