Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds the ability to keep specific fields raw when inserting and updating #59

Closed
wants to merge 1 commit into from
Closed
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
Added "keep raw" ability for inserts and updates.
  • Loading branch information
brianherbert committed May 21, 2012
commit 4797a44e94f0f38100620ad0d8a59d631da809e7
54 changes: 47 additions & 7 deletions idiorm.php
Original file line number Diff line number Diff line change
@@ -141,6 +141,10 @@ class ORM {
// this instance only. Overrides the config settings.
protected $_instance_id_column = null;

// Keys of columns for inserts and updates, that
// should be kept raw.
protected $_keep_raw = array();

// ---------------------- //
// --- STATIC METHODS --- //
// ---------------------- //
@@ -588,8 +592,23 @@ protected function _add_simple_where($column_name, $separator, $value) {
* Return a string containing the given number of question marks,
* separated by commas. Eg "?, ?, ?"
*/
protected function _create_placeholders($number_of_placeholders) {
return join(", ", array_fill(0, $number_of_placeholders, "?"));
protected function _create_placeholders($fields) {

// See if we want to force any given field to be raw
$prep_arr = array();
foreach ($fields as $key => $val) {
if(in_array($key, $this->_keep_raw)) {
// We want to keep it raw
$prep_arr[] = $val;
}else{
// We want to prepare it and not keep it raw
$prep_arr[] = '?';
}
}

$placeholders = join(", ", $prep_arr);

return $placeholders;
}

/**
@@ -671,7 +690,7 @@ public function where_lte($column_name, $value) {
*/
public function where_in($column_name, $values) {
$column_name = $this->_quote_identifier($column_name);
$placeholders = $this->_create_placeholders(count($values));
$placeholders = $this->_create_placeholders($values);
return $this->_add_where("{$column_name} IN ({$placeholders})", $values);
}

@@ -680,7 +699,7 @@ public function where_in($column_name, $values) {
*/
public function where_not_in($column_name, $values) {
$column_name = $this->_quote_identifier($column_name);
$placeholders = $this->_create_placeholders(count($values));
$placeholders = $this->_create_placeholders($values);
return $this->_add_where("{$column_name} NOT IN ({$placeholders})", $values);
}

@@ -1045,7 +1064,15 @@ public function is_dirty($key) {
*/
public function save() {
$query = array();
$values = array_values($this->_dirty_fields);

// Remove any data that we set as raw since it's baked into the query already
$dirty_fields = $this->_dirty_fields;
$values = array();
foreach ($this->_dirty_fields as $column => $val) {
if ( ! in_array($column, $this->_keep_raw)) {
$values[] = $val;
}
}

if (!$this->_is_new) { // UPDATE
// If there are no dirty values, do nothing
@@ -1083,7 +1110,11 @@ protected function _build_update() {

$field_list = array();
foreach ($this->_dirty_fields as $key => $value) {
$field_list[] = "{$this->_quote_identifier($key)} = ?";
$q_or_val = '?';
if (in_array($key, $this->_keep_raw)){
$q_or_val = $value;
}
$field_list[] = "{$this->_quote_identifier($key)} = $q_or_val";
}
$query[] = join(", ", $field_list);
$query[] = "WHERE";
@@ -1102,11 +1133,20 @@ protected function _build_insert() {
$query[] = "(" . join(", ", $field_list) . ")";
$query[] = "VALUES";

$placeholders = $this->_create_placeholders(count($this->_dirty_fields));
$placeholders = $this->_create_placeholders($this->_dirty_fields);

$query[] = "({$placeholders})";

return join(" ", $query);
}

/**
* Keep raw, for fields we don't want to prepare and then set it
*/
public function keep_raw($column_list) {
$this->_keep_raw = (array)$column_list;
}

/**
* Delete this record from the database
*/