Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 70 additions & 36 deletions idiorm.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ class ORM implements ArrayAccess {
// this instance only. Overrides the config settings.
protected $_instance_id_column = null;

// associative results flag
protected $_associative_results = true;


// ---------------------- //
// --- STATIC METHODS --- //
// ---------------------- //
Expand Down Expand Up @@ -194,7 +198,7 @@ public static function configure($key, $value = null, $connection_name = self::D
}
} else {
if (is_null($value)) {
// Shortcut: If only one string argument is passed,
// Shortcut: If only one string argument is passed,
// assume it's a connection string
$value = $key;
$key = 'connection_string';
Expand Down Expand Up @@ -222,7 +226,7 @@ public static function get_config($key = null, $connection_name = self::DEFAULT_
public static function reset_config() {
self::$_config = array();
}

/**
* Despite its slightly odd name, this is actually the factory
* method used to acquire instances of the class. It is named
Expand Down Expand Up @@ -307,7 +311,7 @@ protected static function _setup_identifier_quote_character($connection_name) {

/**
* Detect and initialise the limit clause style ("SELECT TOP 5" /
* "... LIMIT 5"). If this has been specified manually using
* "... LIMIT 5"). If this has been specified manually using
* ORM::configure('limit_clause_style', 'top'), this will do nothing.
* @param string $connection_name Which connection to use
*/
Expand Down Expand Up @@ -459,13 +463,13 @@ protected static function _log_query($query, $parameters, $connection_name) {

self::$_last_query = $bound_query;
self::$_query_log[$connection_name][] = $bound_query;


if(is_callable(self::$_config[$connection_name]['logger'])){
$logger = self::$_config[$connection_name]['logger'];
$logger($bound_query);
}

return true;
}

Expand Down Expand Up @@ -523,6 +527,10 @@ protected function __construct($table_name, $data = array(), $connection_name =
$this->_data = $data;

$this->_connection_name = $connection_name;

// Set the flag as config dictates
$this->_associative_results = self::$_config[$this->_connection_name]['find_many_primary_id_as_key'];

self::_setup_db_config($connection_name);
}

Expand All @@ -542,6 +550,33 @@ public function create($data=null) {
return $this;
}

/**
* Set the ORM instance to return non associative results sets
* @return ORM instance
*/
public function non_associative() {
$this->_associative_results = false;
return $this;
}

/**
* Set the ORM instance to return associative results sets
* @return ORM instance
*/
public function associative() {
$this->_associative_results = true;
return $this;
}

/**
* Set the ORM instance to return associative (or not) results sets, as config dictates
* @return ORM instance
*/
public function reset_associative() {
$this->_associative_results = self::$_config[$this->_connection_name]['find_many_primary_id_as_key'];
return $this;
}

/**
* Specify the ID column to use for this instance or array of instances only.
* This overrides the id_column and id_column_overrides settings.
Expand Down Expand Up @@ -612,10 +647,7 @@ public function find_many() {
*/
protected function _find_many() {
$rows = $this->_run();
if(self::$_config[$this->_connection_name]['find_many_primary_id_as_key']) {
return $this->_instances_with_id_as_key($rows);
}
return array_map(array($this, '_create_instance_from_row'), $rows);
return $this->_instances_with_id_as_key($rows);
}

/**
Expand All @@ -626,10 +658,12 @@ protected function _find_many() {
* @return array
*/
protected function _instances_with_id_as_key($rows) {
$size = count($rows);
$instances = array();
foreach($rows as $row) {
$row = $this->_create_instance_from_row($row);
$instances[$row->id()] = $row;
for ($i = 0; $i < $size; $i++) {
$row = $this->_create_instance_from_row($rows[$i]);
$key = (isset($row->{$this->_instance_id_column}) && $this->_associative_results) ? $row->{$this->_instance_id_column} : $i;
$instances[$key] = $row;
}
return $instances;
}
Expand All @@ -651,7 +685,7 @@ public function find_result_set() {
* @return array
*/
public function find_array() {
return $this->_run();
return $this->_run();
}

/**
Expand Down Expand Up @@ -808,14 +842,14 @@ public function select_expr($expr, $alias=null) {
* Add columns to the list of columns returned by the SELECT
* query. This defaults to '*'. Many columns can be supplied
* as either an array or as a list of parameters to the method.
*
*
* Note that the alias must not be numeric - if you want a
* numeric alias then prepend it with some alpha chars. eg. a1
*
*
* @example select_many(array('alias' => 'column', 'column2', 'alias2' => 'column3'), 'column4', 'column5');
* @example select_many('column', 'column2', 'column3');
* @example select_many(array('column', 'column2', 'column3'), 'column4', 'column5');
*
*
* @return \ORM
*/
public function select_many() {
Expand All @@ -834,16 +868,16 @@ public function select_many() {

/**
* Add an unquoted expression to the list of columns returned
* by the SELECT query. Many columns can be supplied as either
* by the SELECT query. Many columns can be supplied as either
* an array or as a list of parameters to the method.
*
*
* Note that the alias must not be numeric - if you want a
* numeric alias then prepend it with some alpha chars. eg. a1
*
*
* @example select_many_expr(array('alias' => 'column', 'column2', 'alias2' => 'column3'), 'column4', 'column5')
* @example select_many_expr('column', 'column2', 'column3')
* @example select_many_expr(array('column', 'column2', 'column3'), 'column4', 'column5')
*
*
* @return \ORM
*/
public function select_many_expr() {
Expand All @@ -863,11 +897,11 @@ public function select_many_expr() {
/**
* Take a column specification for the select many methods and convert it
* into a normalised array of columns and aliases.
*
*
* It is designed to turn the following styles into a normalised array:
*
*
* array(array('alias' => 'column', 'column2', 'alias2' => 'column3'), 'column4', 'column5'))
*
*
* @param array $columns
* @return array
*/
Expand Down Expand Up @@ -1039,7 +1073,7 @@ protected function _add_simple_condition($type, $column_name, $separator, $value
}
$column_name = $this->_quote_identifier($column_name);
return $this->_add_condition($type, "{$column_name} {$separator} ?", $value);
}
}

/**
* Return a string containing the given number of question marks,
Expand Down Expand Up @@ -1234,7 +1268,7 @@ public function group_by($column_name) {
}

/**
* Add an unquoted expression to the list of columns to GROUP BY
* Add an unquoted expression to the list of columns to GROUP BY
*/
public function group_by_expr($expr) {
$this->_group_by[] = $expr;
Expand Down Expand Up @@ -1685,7 +1719,7 @@ public function set($key, $value = null) {
* To set multiple properties at once, pass an associative array
* as the first parameter and leave out the second parameter.
* Flags the properties as 'dirty' so they will be saved to the
* database when save() is called.
* database when save() is called.
* @param string|array $key
* @param string|null $value
*/
Expand Down Expand Up @@ -1887,12 +1921,12 @@ public function __isset($key) {

/**
* Magic method to capture calls to undefined class methods.
* In this case we are attempting to convert camel case formatted
* In this case we are attempting to convert camel case formatted
* methods into underscore formatted methods.
*
* This allows us to call ORM methods using camel case and remain
* This allows us to call ORM methods using camel case and remain
* backwards compatible.
*
*
* @param string $name
* @param array $arguments
* @return ORM
Expand All @@ -1905,13 +1939,13 @@ public function __call($name, $arguments)
}

/**
* Magic method to capture calls to undefined static class methods.
* In this case we are attempting to convert camel case formatted
* Magic method to capture calls to undefined static class methods.
* In this case we are attempting to convert camel case formatted
* methods into underscore formatted methods.
*
* This allows us to call ORM methods using camel case and remain
* This allows us to call ORM methods using camel case and remain
* backwards compatible.
*
*
* @param string $name
* @param array $arguments
* @return ORM
Expand Down Expand Up @@ -2070,7 +2104,7 @@ public function get_results() {
public function as_array() {
return $this->get_results();
}

/**
* Get the number of records in the result set
* @return int
Expand Down Expand Up @@ -2105,7 +2139,7 @@ public function offsetExists($offset) {
public function offsetGet($offset) {
return $this->_results[$offset];
}

/**
* ArrayAccess
* @param int|string $offset
Expand Down
1 change: 1 addition & 0 deletions test/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public function testGetConfigArray() {
'caching' => false,
'return_result_sets' => false,
'limit_clause_style' => 'limit',
'find_many_primary_id_as_key' => true
);
$this->assertEquals($expected, ORM::get_config());
}
Expand Down