Skip to content

Commit

Permalink
add updateAll method to batch-update multiple records, #285
Browse files Browse the repository at this point in the history
  • Loading branch information
ikkez committed Nov 24, 2019
1 parent a89cad9 commit 397b6cd
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions db/sql/mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,41 @@ function update() {
return $this;
}

/**
* batch-update multiple records at once
* @param string|array $filter
* @return int
*/
function updateAll($filter=NULL) {
$args=[];
$ctr=$out=0;
$pairs='';
foreach ($this->fields as $key=>$field)
if ($field['changed']) {
$pairs.=($pairs?',':'').$this->db->quotekey($key).'=?';
$args[++$ctr]=[$field['value'],$field['pdo_type']];
}
if ($filter)
if (is_array($filter)) {
$cond=array_shift($filter);
$args=array_merge($args,$filter);
$filter=' WHERE '.$cond;
} else
$filter=' WHERE '.$filter;
if ($pairs) {
$sql='UPDATE '.$this->table.' SET '.$pairs.$filter;
$out = $this->db->exec($sql,$args);
}
// reset changed flag after calling afterupdate
foreach ($this->fields as $key=>&$field) {
$field['changed']=FALSE;
$field['initial']=$field['value'];
unset($field);
}
return $out;
}


/**
* Delete current record
* @return int
Expand Down

0 comments on commit 397b6cd

Please sign in to comment.