1

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' )
    );

}
Tarek
  • 3,810
  • 3
  • 36
  • 62

1 Answers1

1

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

pocesar
  • 6,860
  • 6
  • 56
  • 88
  • I thought it was possible to modify the model definition to make it work. – Tarek Dec 28 '12 at 12:56
  • update your post with your student model definition and I might help you out. I need to make sure if you are using the naming conventions of the ORM module or not – pocesar Dec 28 '12 at 20:26
  • I'm able to load the student from File , the problem comes when i try to load the student using FileSessions – Tarek Dec 28 '12 at 22:16