Fetching Objects

Fetch

The most basic way to fetch objects from a model is to call the fetch method. Fetch takes the primary key and fetches the corresponding Record object. It returns NULL if the object could not be found.

$obj = $this->my_objects->fetch($id);
if (!$obj) {
    echo "Not Found";
}

You can also pass an array of primary keys to fetch and it will return a collection containing the records you've specified. If no records are found, an empty array is returned

$objects = $this->my_objects->fetch(array($id1, $id2));
if (!$objects) {
    echo "Not Found";
}

FetchAll

The FetchAll method fetches all of the available records and returns a collection if records are found, or an empty array if there are none.

$objects = $this->my_objects->fetchAll();
if (!$objects) {
    echo "Not Found";
}

Fetch Options

fetchAll() takes an array of options that can specify which objects to return. You can also specify the same options as the second parameter of fetch(). If you only want to fetch one record, you can pass these options to fetchOne.

$obj = $this->my_objects->fetchOne(array(
    'where' => array('id = ?' => $id)
    );

This example is equivelent to using fetch().

$obj = $this->my_objects->fetch($id);

where

// Add a where clause to your fetch
$obj = $this->my_objects->fetchAll(array(
    'where'=>array('column=?'=>$value)
));
 
// Multiple where conditions with AND as the operator
$obj = $this->my_objects->fetchAll(array(
    'where'=>array('column=?'=>$value, 'other_column=?'=>$other_value)
));

cols

// Specify the columns you want returned from the fetch
$obj = $this->my_objects->fetchAll(array(
    'cols'=>array('foo', 'bar', 'baz')
));

distinct

// A boolean flag to make the fetch distinct
$obj = $this->my_objects->fetchAll(array(
    'distinct'=>true
));

join

// Adds a single arbitrary JOIN to the fetch.
$join = array(
    'type' => 'left',
    'name' => 'table_b',
    'cond' => 'table_b.a_id = table_a.id',
    'cols' => array('foo', 'bar', 'baz')
);
$obj = $this->my_objects->fetchAll(array(
    'join'=>$join
));

eager

Solar models use lazy-fetching of related records by default. This can lead to an excessive number of queries (N+1) because the native table is queried first, then, as you loop through each record requesting the related records, the related table will be queried. To solve this, you can specify eager-fetching on related tables. This will issue a WHERE IN () clause or an INNER JOIN (SELECT…) on larger sets (See wherein_max). As a result, eager-fetching performs only one additional query.

// Eager-fetch the specified column(s)
$obj = $this->my_objects->fetchAll(array(
    'eager'=>array('table')
));

group

// Adds GROUP BY columns to the fetch
$obj = $this->my_objects->fetchAll(array(
    'group'=>array('column', 'other_column')
));

having

// Specify how results should be ordered
$obj = $this->my_objects->fetchAll(array(
    'group'=>array('column'),
    'having'=>array('count(id) > ?'=>$count)
));

order

// Specify how results should be ordered
$obj = $this->my_objects->fetchAll(array(
    'order'=>array('column ASC', 'other_column DESC')
));

paging

// Set the rows per page option. Need to use with the page option
$obj = $this->my_objects->fetchAll(array(
    'page'=>1,
    'paging'=>50
));

page

// using the default model _paging of 10
$obj = $this->my_objects->fetchAll(array(
    'page'=>1
));
 
// using different paging
$obj = $this->my_objects->fetchAll(array(
    'page'=>1,
    'paging'=>50
));

count_pages

// follow the fetch with a query to count the total number of records and pages
// Useful when paginating your results in a view
$obj = $this->my_objects->fetchAll(array(
    'page'=>1,
    'paging'=>50,
    'count_pages'=>true
));

limit

// Limit with no offset
$obj = $this->my_objects->fetchAll(array(
    'limit'=>10
));
 
// Limit with offset
$obj = $this->my_objects->fetchAll(array(
    'limit'=>array(10,30)
));

bind

$obj = $this->my_objects->fetchAll(array(
    'where'=>array('column=:column'),
    'bind'=>array('column'=>$value)
));

cache

cache_key

alias

// Sets the alias to use for this eager or fetch. 
// Does not work on a generic $model->fetch($id) because ORDER is overridden and does not use the new alias
$obj = $this->my_objects->fetchAll(array(
    'alias'=>'myalias')
));
 
manual/model/fetching_objects.txt · Last modified: 2009/11/06 16:08 by jelofson