// classes/models/persons.php
// class Model_Persons extends ActiveRecord\Model {}
// classes/controllers/test.php
// function action_test()
$person_name = 'Abc Def';
$options = array('include' => array('tables', 'chairs'));
$person = Model_Persons::find_by_person_name($person_name, $options);
if ($person !== null)
{
echo $table = $person->table_name;
echo $chair = $person->chair_name;
}
$query = DB::select('*')
->from('table')
->join('chair', 'left')
->on('table.id', '=', 'chair.table_id') // chair belongs to a table
->join('person', 'left')
->on('chair.id', '=', 'person.chair_id') // person belongs to a chair
->execute()
->as_array();
// By using as_array you can simply print_r($query);
// to see the query results. Then when your happy use as_object to
// reference it like $field->value
//
// Don't forget unless you alias every thing you will have no id
// for table or chair as it is not a unique field name.
Change the tables is no option. In my example case it is not belongs to belongs to but one person can choose form multiple chairs and multiple tables. Your way is totally different.
http://lukebaker.org/projects/activerecord-in-php/documentation/
Nice documentation but I still can not figure out to make multiple joins with AR.
class Model_Persons extends ActiveRecord\Model {
protected $belongs_to = array('chair', 'table');
}
class Model_Chairs extends ActiveRecord\Model {
protected $has_many = array('persons',
array('tables' => array('through' => 'persons')));
}
class Model_Tables extends ActiveRecord\Model {
protected $has_many = array('persons',
array('chairs' => array('through' => 'persons')));
}
$person = Model_Persons::find_by_person_name($person_name);
class Model_Person extends ActiveRecord\Model {
protected $has_one = array('chair', 'table');
// protected $has_many = array();
// protected $belongs_to = array();
}
class Model_Chair extends ActiveRecord\Model {
// protected $has_one = array();
protected $has_many = array('persons');
// protected $belongs_to = array();
}
class Model_Table extends ActiveRecord\Model {
// protected $has_one = array();
protected $has_many = array('persons');
// protected $belongs_to = array();
}
$person = Model_Person::find_by_person_name($person_name);
echo $person->table->table_name; echo $person->chair->chair_name;
$options = array('include' => array('table', 'chair'));
$person = Model_Person::find_by_person_name($person_name, $options);
echo $person->table->table_name; echo $person->chair->chair_name;
It looks like you're new here. If you want to get involved, click one of these buttons!