Example #1
0
 protected function select($parameters = [])
 {
     global $app;
     if (!$this->_limit && isset($parameters[":limit"])) {
         $this->limit($parameters[":limit"]);
     }
     $sql = $this->buildSelectSQL();
     $results = $app->db()->query($this->_variables, $sql, $parameters);
     $results_per_row = count($this->_columns);
     $output = [];
     // Go through the results one at a time
     if ($results) {
         foreach ($results as $row) {
             $objects = [];
             $object_params = [];
             // Collect all the results into separate tables
             for ($i = 0; $i < $results_per_row; $i++) {
                 $table = $this->_columns[$i]["table"];
                 $column = $this->_columns[$i];
                 $object_params[$table][$column["id"]] = $this->coerceToPHP($row[$i], $column["type"]);
             }
             // Now turn each set of object_params into an object
             foreach ($object_params as $table => $values) {
                 $class_name = ucwords($table, "_");
                 $objects[$table] = method_exists($class_name, "Make") ? $class_name::Make($values) : new $class_name($values);
                 $objects[$table]->cleanSlots();
             }
             // Now stitch up the foreigns
             foreach ($objects as $table => $object) {
                 $columns = Database::FindAllColumns($table);
                 foreach ($columns as $column => $data) {
                     if (isset($data["foreign"]) && isset($objects[$data["foreign"]])) {
                         $object->{$column} = $objects[$data["foreign"]];
                         $object->cleanSlots();
                     }
                 }
             }
             // Save the base object
             if (isset($objects[$this->_name])) {
                 $output[] = $objects[$this->_name];
             }
         }
         return $output;
     } else {
         return [];
     }
 }