Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to join tables in Orm relation
  • How to join hotel and master tables to get "hotel name" which is searching by hotel name in text box.
    Hotel name column is only include in hotel table.

    Tables:
    hotel(hotel_code,hotel_name)
    master_account(master_code,hotel_code)

    my code is,(...fuel\app\modules\account\classes\controller\account.php)
    $data['master_accounts'] = \Model_Master_Account::find('all', array(
       'related' => array(
           'hotel' => array(
               'join_type' => 'left',
               'join_on' => array(
                   array('hotel_code', '=', \DB::select('hotel_code')->from(\Model_Hotel::table())->where('hotel_name', 'like','%'.$hotel.'%')),
               ),
           ),
       ),
       'order_by' => array('master_code' => 'desc'),
    )
    );
  • HarroHarro
    Accepted Answer
    ORM queries are left joins by default.

    I personally never use find() and array notation, as it becomes messy and difficult to read very quickly, as you can see. ;-)

    This should work fine:

    $data['master_accounts']  =\Model_Master_Account::query()
        ->related('hotel')
        ->where('hotel.hotel_name', 'like','%'.$hotel.'%')
        ->order_by('master_code', 'desc')
        ->get();
  • Yeah,got it.
    Thank you,Mr.Harro Verton :-)

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion