Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added operators to the get() method #2

Merged
merged 1 commit into from

2 participants

@hotestimator

Added the possibility of operators to the get method. eg:

$options = array(
 'status >' => 5
);
$my_data = $this->model->get($options);
@ccschmitz ccschmitz merged commit fb87cce into ccschmitz:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 15, 2012
  1. @hotestimator
This page is out of date. Refresh to see the latest.
Showing with 22 additions and 4 deletions.
  1. +22 −4 lib/MY_Model.php
View
26 lib/MY_Model.php
@@ -125,6 +125,21 @@ function add($options = array())
*/
function get($options = array())
{
+ // set an array for field querys and values
+ // This allows gets with operators
+ // $options = array('status >' => 5)
+ $option_fields = array();
+ foreach($options as $key => $value)
+ {
+ $parts = explode(' ', $key, 2);
+
+ $field = isset($parts[0]) ? $parts[0] : '';
+ $operator = isset($parts[1]) ? $parts[1] : '';
+
+ $option_fields[$field]['query'] = $key;
+ $option_fields[$field]['value'] = $value;
+ }
+
$defaults = array(
'sort_direction' => 'asc'
);
@@ -134,9 +149,9 @@ function get($options = array())
foreach ($this->fields as $field)
{
- if (isset($options[$field]))
+ if (isset($option_fields[$field]))
{
- $this->db->where($field, $options[$field]);
+ $this->db->where($option_fields[$field]['query'], $option_fields[$field]['value']);
}
}
@@ -246,9 +261,12 @@ function _validate_options_exist($options)
{
foreach ($options as $key => $value)
{
- if ( ! $this->db->field_exists($key, $this->primary_table))
+ $parts = explode(' ', $key);
+ $field = $parts[1];
+
+ if ( ! $this->db->field_exists($field, $this->primary_table))
{
- show_error('You are trying to insert data into a field that does not exist. The field "'. $key .'" does not exist in the "'. $this->primary_table .'" table.');
+ show_error('You are trying to insert data into a field that does not exist. The field "'. $field .'" does not exist in the "'. $this->primary_table .'" table.');
}
}
}
Something went wrong with that request. Please try again.