DataMapper ORM


Get (Alternatives)

Datamapper ORM includes a number of alternatives to the normal get methods. These can be used to perform more advanced operations based on which one is used.

Also see the Simple Cache Extension for another get variation.

Subsections:

$object->get_iterated()

This method can be used to reduce memory overhead and even increase performance if you know you only want to loop over the result set. In fact, it is recommended in almost all cases where you expect more than one result, and don't need direct access to one or more results.

The get_iterated method works by creating a PHP Iterator. The iterator creates each object as it is needed; there is actually only one object that is shared as you loop over the set.

Special thanks to TheJim for coming up with the prototype for this method.

Arguments

Note: The results of this method do not directly modify $object.

Therefore, the database fields are not set on $object, and the $all array will be empty.

However, you can still check the results of the query using exists() and result_count().

Common Usage

$countries = new Country();
$countries->get_iterated();
if(!$countries->exists()) {
    echo('No countries found');
} else {
    echo($countries->result_count() . ' countries were found.');
    foreach($countries as $country) {
        // process like normal
    }
}

When To Use Get Iterated (Over Get)

There are several cases where you should always use get, and several cases where get_iterated is preferred.

$object->get_paged()

Example Application

You can see this feature used in:

ZIP/examples/application/
  » controllers/bugs.php
  » views/bugs/paging.php

This method is used to easily get a query that is broken up into pages, as well as handle getting the total number of rows.

You can use this method with any query parameters, and those parameters will automatically be used to determine the total number of rows.

Note: Each call to this method will result in two queries: one for the data set, and one to count the total number of rows.

Arguments

When requesting a page number by row, get_paged converts the row number to the correct page for that row. For example, if you had 10 rows per page, and requested row number 12, the page will be 2, and the starting row will be 10.

This method will automatically prevent the request of a page that is outside the queries' boundaries. If the requested page is less than 1, it reverts to the first page. If the requested page is greater than the total number of pages, it reverts to the last page.

What makes this method so useful, however, is what else it returns. Each call sets the following properties on an object, which is called $paged by default.

If you already have a field called paged, you can easily change the object to a different name with the last argument.

Example

// IN THE CONTROLLER
function archive($page = 1)
{
    $posts = new Post();
    // show newest first
    $posts->order_by('created', 'DESC');
    // show 10 posts per page
    $posts->get_paged($page, 10);

    // send to view
    $this->load->view('posts/archive', array('posts' => $posts));
}

// IN THE VIEW
foreach($posts as $post)
{
    // render the post
}
if($posts->paged->has_previous)
{
    ?>
<a href="<?= site_url('posts/archive/1' ?>">&lt;&lt; First</a>
<a href="<?= site_url('posts/archive/'.$posts->paged->previous_page) ?>">&lt; Prev</a>
    <?
}
if($posts->paged->has_next)
{
    ?>
<a href="<?= site_url('posts/archive/'.$posts->paged->next_page ?>">Next &gt;</a>
<a href="<?= site_url('posts/archive/'.$posts->paged->total_pages) ?>">Last &gt; &gt;</a>
    <?
}

$object->get_paged_iterated()

A simple combination of get_paged and get_iterated, allowing you to efficiently loop over the results of a paged query.

Use the exact same as get_paged, except the results can only be looped over. Strongly recommended in most cases.

$object->get_raw()

This method runs the query, but returns the raw results from CodeIgniter's database library. It also clears the current query immediately.

Arguments

$foo = new Foo();
$foo->include_related('bar', 'name');
$foo->where_related('bar', 'name', 'baz');
$query = $foo->get_raw();
foreach($query->result() as $row) {
    echo $row->bar_name;
}

$object->get_sql()

This method returns the SQL for the currently built query, as if get() was called. It clears the current query immediately.

Arguments

$u = new User();
$u->where('name', 'Bob');
echo $u->get_sql();
// outputs the raw SQL

Example with relationship

$group = new Group(1); // load Group #1

echo $group->user->get_sql();
// SELECT `users`.*
// FROM `users`

echo $group->user->get_sql(NULL, NULL, TRUE);
// SELECT `users`.*
// FROM `users`
// LEFT OUTER JOIN `groups` groups ON `users`.`group_id` = `groups.id`
// WHERE `groups`.`id` = 1