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

Update ORDataObject persist method #4862

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 46 additions & 25 deletions src/Common/ORDataObject/ORDataObject.php
Expand Up @@ -31,44 +31,65 @@ public function __construct($table = null, $prefix = null)

public function persist()
{
// NOTE: REPLACE INTO does a DELETE and then INSERT, if you have foreign keys setup the delete call will trigger
$sql = "REPLACE INTO " . $this->_prefix . $this->_table . " SET ";
//echo "<br /><br />";
$fields = QueryUtils::listTableFields($this->_table);
$this_table = escape_table_name($this->_prefix . $this->_table);
$db = get_db();
$pkeys = $db->MetaPrimaryKeys($this->_table);
$fields = $db->metaColumns($this_table);

foreach ($fields as $field) {
$this_rec = [];
$pkeys = [];
$pkey_id = '';
foreach ($fields as $field => $objAdoField) {
$func = "get_" . $field;
//echo "f: $field m: $func status: " . (is_callable(array($this,$func))? "yes" : "no") . "<br />";
if (is_callable(array($this,$func))) {
$val = call_user_func(array($this,$func));

if (in_array($field, $pkeys) && empty($val)) {
$last_id = generate_id();
call_user_func(array(&$this,"set_" . $field), $last_id);
$val = $last_id;
if ($objAdoField->primary_key) {
// Potential issue if multiple fields included as primary key
$pkey_id = $field;
if (empty($val) && ($objAdoField->auto_increment)) {
// Skip the field from sql statement.
continue;
} else {
$val = generate_id();
}
$pkeys[] = $field;
}

if (!empty($val)) {
//echo "s: $field to: $val <br />";

//modified 01-2010 by BGM to centralize to formdata.inc.php
// have place several debug statements to allow standardized testing over next several months
$sql .= " `" . $field . "` = '" . add_escape_custom(strval($val)) . "',";
//DEBUG LINE - error_log("ORDataObject persist after escape: ".add_escape_custom(strval($val)), 0);
//DEBUG LINE - error_log("ORDataObject persist after escape and then stripslashes test: ".stripslashes(add_escape_custom(strval($val))), 0);
//DEBUG LINE - error_log("ORDataObject original before the escape and then stripslashes test: ".strval($val), 0);
$this_rec[$field] = $val;
}
}
}
if (empty($this_rec)) {
// WTF?
return false;
}

$update = false;
if (!empty($pkeys)) {
// Find record with matching primary keys
$sql = sprintf(
'select * FROM `%s` WHERE %s=?',
$this_table,
implode("=?, ", $pkeys)
);
$rs = $db->execute($sql, array_values(array_intersect_key($this_rec, array_flip($pkeys))));
$update = ($rs->recordCount() > 0);
}

if (strrpos($sql, ",") == (strlen($sql) - 1)) {
$sql = substr($sql, 0, (strlen($sql) - 1));
if ($update) {
$sql = $db->getUpdateSql($rs, $this_rec);
if (!$sql) {
// Nothing to update
return false;
} else {
sqlQuery($sql);
}
} else {
$sql = $db->getInsertSql($this_table, $this_rec);
// Capture inserted autoincremented value
$pkey = sqlInsert($sql);
call_user_func([$this, 'set_'.$pkey_id], $pkey);
}

//echo "<br />sql is: " . $sql . "<br /><br />";
sqlQuery($sql);
return true;
}

Expand Down