Search code examples
phpkohanakohana-orm

How to fetch columns from joined table in Kohana 2.x


I'm using kohana 2.x for my project,I've following query

 $result = ORM::factory('table1')
                        ->join('table2','table1.id','table2.id')
                        ->find_all();

Using above query I can fetch data from table1,but I need fetch data from table2 also.When I'm giving like this

$result->table2_column_name;

It showing error,how can I fetch the data from table2.


Solution

  • Your statement is neither ORM or Active Record. If you have a Table1_Model class that extends ORM class then you can use ORM::factory('table1'); You also need almost same thing for table2. And for each class there should be relations declared. Getting Started with ORM

    The resultant models will be,

    class Table1_Model extends ORM {
        protected $has_one = array('table2');
    }
    
    class Table2_Model extends ORM {
        protected $has_one = array('table1');
    }
    

    Now you can query like this,

    $result = ORM::factory('table1', 1);
    echo $result->table1_column_name;
    echo $result->table2->table2_column_name;