/** * Select record(s) in table * * <code> * $records = $users->select('[id=2]'); * $records = $users->select(null, 'all'); * $records = $users->select(null, 'all', null, array('login')); * $records = $users->select(null, 2, 1); * </code> * * @param string $query XPath query * @param integer $row_count Row count. To select all records write 'all' * @param integer $offset Offset * @param array $fields Fields * @param string $order_by Order by * @param string $order Order type * @return array */ public function select($query = null, $row_count = 'all', $offset = null, array $fields = null, $order_by = 'id', $order = 'ASC') { // Redefine vars $query = $query === null ? null : (string) $query; $offset = $offset === null ? null : (int) $offset; $order_by = (string) $order_by; $order = (string) $order; // Execute query if ($query !== null) { $tmp = $this->table['xml_object']->xpath('//' . $this->name . $query); } else { $tmp = $this->table['xml_object']->xpath($this->name); } // Init vars $data = array(); $records = array(); $_records = array(); $one_record = false; // If row count is null then select only one record // eg: $users->select('[login="******"]', null); if ($row_count == null) { if (isset($tmp[0])) { $_records = $tmp[0]; $one_record = true; } } else { // If row count is 'all' then select all records // eg: // $users->select('[status="active"]', 'all'); // or // $users->select('[status="active"]'); foreach ($tmp as $record) { $data[] = $record; } $_records = $data; } // If array of fields is exits then get records with this fields only if (count($fields) > 0) { if (count($_records) > 0) { $count = 0; foreach ($_records as $key => $record) { foreach ($fields as $field) { $record_array[$count][$field] = (string) $record->{$field}; } $record_array[$count]['id'] = (int) $record->id; if ($order_by == 'id') { $record_array[$count]['sort'] = (int) $record->{$order_by}; } else { $record_array[$count]['sort'] = (string) $record->{$order_by}; } $count++; } // Sort records $records = Table::subvalSort($record_array, 'sort', $order); // Slice records array if ($offset === null && is_int($row_count)) { $records = array_slice($records, -$row_count, $row_count); } elseif ($offset !== null && is_int($row_count)) { $records = array_slice($records, $offset, $row_count); } } } else { // Convert from XML object to array if (!$one_record) { $count = 0; foreach ($_records as $xml_objects) { $vars = get_object_vars($xml_objects); foreach ($vars as $key => $value) { $records[$count][$key] = (string) $value; if ($order_by == 'id') { $records[$count]['sort'] = (int) $vars['id']; } else { $records[$count]['sort'] = (string) $vars[$order_by]; } } $count++; } // Sort records $records = Table::subvalSort($records, 'sort', $order); // Slice records array if ($offset === null && is_int($row_count)) { $records = array_slice($records, -$row_count, $row_count); } elseif ($offset !== null && is_int($row_count)) { $records = array_slice($records, $offset, $row_count); } } else { // Single record $vars = get_object_vars($_records[0]); foreach ($vars as $key => $value) { $records[$key] = (string) $value; } } } // Return records return $records; }