find_many() 공개 메소드

Wrap Idiorm's find_many method to return an array of instances of the class associated with this wrapper instead of the raw ORM class.
public find_many ( ) : Array
리턴 Array
예제 #1
0
파일: CRUD.php 프로젝트: cal127/phpcrud
 /**
  * @arg bool raw
  * if raw is true, relational fields are returned their ids.
  */
 public function getRows($raw = false)
 {
     $rows = array();
     foreach ($this->orm->find_many() as $row) {
         foreach ($this->fields as $field) {
             // skip the only non-readable field type:
             if (in_array($field['type'], array('file', 'file_multiple')) || !$field['read']) {
                 continue;
             }
             if ($field['type'] == 'flag') {
                 $map = array('No', 'Yes');
                 $rows[$row->id][$field['name']] = $raw ? $row->{$field}['name'] == '0' ? false : true : $map[$row->{$field}['name']];
                 continue;
             }
             if ($field['type'] != 'rel') {
                 $rows[$row->id][$field['name']] = (string) $row->{$field}['name'];
                 continue;
             }
             switch ($field['rel']['type']) {
                 case 'belongs_to':
                 case 'has_one':
                     if ($raw) {
                         if ($field['rel']['type'] == 'belongs_to') {
                             $val = $row->{$field['rel']['key1']};
                         } else {
                             if ($rel_obj = $row->{$field['rel']['relator_mtd']}()) {
                                 $val = $rel_obj->id;
                             } else {
                                 $val = null;
                             }
                         }
                         break;
                     }
                     if (!($rel_obj = $row->{$field['rel']['relator_mtd']}()) || !($val = $rel_obj->{$field['rel']['related_model_field']})) {
                         $val = null;
                     }
                     break;
                 case 'has_many':
                 case 'has_many_through':
                     if ($rel_obj = $row->{$field['rel']['relator_mtd']}()) {
                         $all_related_values = $rel_obj->find_array();
                     } else {
                         $val = null;
                         break;
                     }
                     if ($raw) {
                         $related_model_field = 'id';
                     } else {
                         $related_model_field = $field['rel']['related_model_field'];
                     }
                     $val = array_map(function ($row) use($related_model_field) {
                         return $row[$related_model_field];
                     }, $all_related_values);
                     break;
             }
             $rows[$row->id][$field['name']] = $val;
         }
         $rows[$row->id] = self::applyCallbacks('pre_read', 1, $rows[$row->id], $this->fields);
     }
     return $rows;
 }