Created
April 7, 2012 15:52
-
-
Save daGrevis/2329883 to your computer and use it in GitHub Desktop.
Fake-load Auto-modeler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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); | |
| } | |
| } |
Author
Author
...with keeping cool Auto-modeler features -- see get_author(). I.e., it returns author that is saved as object property.
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“. :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instead of selecting everything.