I'm trying add a 'where' clause to a query based on an incoming get variable but having problems. The below example seems like it should work but outputs all the events, not just the first when the 'status' variable is set to 'first'. Any ideas?
public function action_test() {
$status = Input::get('status');
// Start query
$query = Model_Events::query();
// Add appropriate where clause to query
if ($status == 'first') {
$query->where('id', '1');
} else {
$query->where('id', '2');
}
// Execute query
$query = Model_Events::query()->get();
// Typecast the query results to an array for output
When you call Model_Whatever::query(), a new instance of Orm\Query is returned. So $query = Model_Events::query()->get(); overwrites the previously created and filtered Query instance. $query->get() is the right way.
sagikazarmakr, yes, the original post was instantiating the ORM twice. My second post addressed it and contains the fix. I expected the the example below to output the contents of the query ...it doesn't. I spent a lot of time trying to figure this out and thought I'd share. (This could be useful to others in the FUELPHP ORM documentation).