Skip to content

Instantly share code, notes, and snippets.

@daGrevis
Created April 7, 2012 15:52
Show Gist options
  • Select an option

  • Save daGrevis/2329883 to your computer and use it in GitHub Desktop.

Select an option

Save daGrevis/2329883 to your computer and use it in GitHub Desktop.
Fake-load Auto-modeler
<?php
class AutoModeler extends AutoModeler_Core {
/**
* Sets data and changes state to loaded.
*
* @param array Data
*
* @author daGrevis
*/
function set_data(array $data) {
$this->_data = $data;
$this->_state = AutoModeler::STATE_LOADED;
}
}
class Model_Guestbook_Entry extends AutoModeler {
protected $_table_name = 'guestbook_entries';
protected $_data = array(
'id' => '',
'author' => '',
'content' => '',
'created_at' => '',
);
function get_author_by_id($id) {
return DB::select('author')->from($this->_table_name)->where('id', '=', $id)->execute()->get('author');
}
function get_author() {
return !$this->loaded() ?: $this->author;
}
}
class Controller_Index extends Controller {
function action_index() {
$guestbook_entry = AutoModeler::factory('Guestbook_Entry');
$id = 1;
$author = $guestbook_entry->get_author_by_id($id);
$guestbook_entry->set_data(array(
'id' => $id,
'author' => $author,
));
$body = $guestbook_entry->get_author();
$this->response->body($body);
ProfilerToolbar::render(true);
}
}
@daGrevis

daGrevis commented Apr 7, 2012

Copy link
Copy Markdown
Author
SELECT `author` FROM `guestbook_entries` WHERE `id` = 1

Instead of selecting everything.

@daGrevis

daGrevis commented Apr 7, 2012

Copy link
Copy Markdown
Author

...with keeping cool Auto-modeler features -- see get_author(). I.e., it returns author that is saved as object property.

@daGrevis

daGrevis commented Apr 7, 2012

Copy link
Copy Markdown
Author

Here are real-life time benchmarks:

0.14810503959656 // SELECT * FROM `foobars` LIMIT 100
0.00021490097045898 // SELECT `id`, `title` FROM `foobars` LIMIT 100

So it does matter!!

P.S. Worth to mention that Auto-Modeler, instead of Kohana ORM, selects by stating all columns not using “star character“. :)

@daGrevis

daGrevis commented Apr 7, 2012

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment