Only fetch the rows when needed

This commit is contained in:
Michael 2017-05-01 05:45:36 +00:00
parent f1c53530a1
commit 0d7996d852

View file

@ -812,23 +812,37 @@ class dba {
// Is there a relation entry for the table? // Is there a relation entry for the table?
if (isset(self::$relation[$table])) { if (isset(self::$relation[$table])) {
foreach (self::$relation[$table] AS $field => $rel_def) { foreach (self::$relation[$table] AS $field => $rel_def) {
// Fetch all rows that are to be deleted // Currently we only support relations with a single variable
$sql = "SELECT ".self::$dbo->escape($field)." FROM `".$table."` WHERE `". if (count($rel_def) > 1) {
implode("` = ? AND `", array_keys($param))."` = ?"; return false;
}
$retval = false; $rel_field = array_values($rel_def)[0];
$data = self::p($sql, $param); $rel_table = array_keys($rel_def)[0];
while ($row = self::fetch($data)) {
foreach ($rel_def AS $rel_table => $rel_field) { // When the search field is the relation field, we don't need to fetch the rows
// We do a separate delete process per row // This is useful when the leading record is already deleted in the frontend but the rest is done in the backend
if ((count($param) == 1) AND ($field == array_keys($param)[0])) {
$retval = self::delete($rel_table, array($rel_field => array_values($param)[0]), true, $callstack);
if (!$retval) {
return false;
}
} else {
// Fetch all rows that are to be deleted
$sql = "SELECT ".self::$dbo->escape($field)." FROM `".$table."` WHERE `".
implode("` = ? AND `", array_keys($param))."` = ?";
$retval = false;
$data = self::p($sql, $param);
while ($row = self::fetch($data)) {
// We have to do a separate delete process per row
$retval = self::delete($rel_table, array($rel_field => $row[$field]), true, $callstack); $retval = self::delete($rel_table, array($rel_field => $row[$field]), true, $callstack);
if (!$retval) { if (!$retval) {
return false; return false;
} }
} }
} if (!$retval) {
if (!$retval) { return true;
return true; }
} }
} }
} }