Last active
August 29, 2015 14:00
-
-
Save vcarl/11152248 to your computer and use it in GitHub Desktop.
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
| public $findMethods = array('indexed' => true); | |
| /** | |
| * Similar to find('list'), but allows for multiple fields to be retrieved. | |
| */ | |
| protected function _findIndexed($state, $query, $results = array()) { | |
| if ($state === 'before') { | |
| return $query; | |
| } | |
| if ($state == 'after') { | |
| // Temporary array for returning data. | |
| $return = array(); | |
| foreach ($results as $key => $result) { | |
| // If there were fields set, use the first one listed as the index. Otherwise, use the id. | |
| if (!empty($query['fields'])) { | |
| $index = $query['fields'][0]; | |
| } else { | |
| $index = 'id'; | |
| } | |
| $data = $result[$this->alias]; | |
| $return[$data[$index]] = $data; | |
| // Unset the key that's now being used as the index, so the data isn't duplicated. | |
| unset($return[$data[$index]][$index]); | |
| // Unset the data that was just copied over from the results. | |
| unset($results[$this->alias][$key]); | |
| } | |
| return $return; | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
e.g. if you have the following data in a database:
and did a find('indexed', array('fields' => array('id', 'username', 'password'), it would return the following data: