mirror of
https://github.com/processwire/processwire.git
synced 2025-08-15 11:14:12 +02:00
DB query updates in WireSaveableItems class
This commit is contained in:
@@ -232,25 +232,27 @@ abstract class WireSaveableItems extends Wire implements \IteratorAggregate {
|
|||||||
public function ___save(Saveable $item) {
|
public function ___save(Saveable $item) {
|
||||||
|
|
||||||
$blank = $this->makeBlankItem();
|
$blank = $this->makeBlankItem();
|
||||||
if(!$item instanceof $blank) throw new WireException("WireSaveableItems::save(item) requires item to be of type '" . $blank->className() . "'");
|
|
||||||
|
|
||||||
$database = $this->wire('database');
|
if(!$item instanceof $blank) {
|
||||||
|
$className = $blank->className();
|
||||||
|
throw new WireException("WireSaveableItems::save(item) requires item to be of type: $className");
|
||||||
|
}
|
||||||
|
|
||||||
|
$database = $this->wire()->database;
|
||||||
$table = $database->escapeTable($this->getTable());
|
$table = $database->escapeTable($this->getTable());
|
||||||
$sql = "`$table` SET ";
|
$sql = "`$table` SET ";
|
||||||
$id = (int) $item->id;
|
$id = (int) $item->id;
|
||||||
$this->saveReady($item);
|
$this->saveReady($item);
|
||||||
$data = $item->getTableData();
|
$data = $item->getTableData();
|
||||||
|
$binds = array();
|
||||||
|
|
||||||
foreach($data as $key => $value) {
|
foreach($data as $key => $value) {
|
||||||
if(!$this->saveItemKey($key)) continue;
|
if(!$this->saveItemKey($key)) continue;
|
||||||
if($key == 'data') {
|
if($key === 'data') $value = is_array($value) ? $this->encodeData($value) : '';
|
||||||
if(is_array($value)) {
|
|
||||||
$value = $this->encodeData($value);
|
|
||||||
} else $value = '';
|
|
||||||
}
|
|
||||||
$key = $database->escapeTableCol($key);
|
$key = $database->escapeTableCol($key);
|
||||||
$value = $database->escapeStr("$value");
|
$bindKey = $database->escapeCol($key);
|
||||||
$sql .= "`$key`='$value', ";
|
$binds[":$bindKey"] = $value;
|
||||||
|
$sql .= "`$key`=:$bindKey, ";
|
||||||
}
|
}
|
||||||
|
|
||||||
$sql = rtrim($sql, ", ");
|
$sql = rtrim($sql, ", ");
|
||||||
@@ -258,15 +260,21 @@ abstract class WireSaveableItems extends Wire implements \IteratorAggregate {
|
|||||||
if($id) {
|
if($id) {
|
||||||
|
|
||||||
$query = $database->prepare("UPDATE $sql WHERE id=:id");
|
$query = $database->prepare("UPDATE $sql WHERE id=:id");
|
||||||
|
foreach($binds as $key => $value) {
|
||||||
|
$query->bindValue($key, $value);
|
||||||
|
}
|
||||||
$query->bindValue(":id", $id, \PDO::PARAM_INT);
|
$query->bindValue(":id", $id, \PDO::PARAM_INT);
|
||||||
$result = $query->execute();
|
$result = $query->execute();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
$query = $database->prepare("INSERT INTO $sql");
|
$query = $database->prepare("INSERT INTO $sql");
|
||||||
|
foreach($binds as $key => $value) {
|
||||||
|
$query->bindValue($key, $value);
|
||||||
|
}
|
||||||
$result = $query->execute();
|
$result = $query->execute();
|
||||||
if($result) {
|
if($result) {
|
||||||
$item->id = $database->lastInsertId();
|
$item->id = (int) $database->lastInsertId();
|
||||||
$this->getAll()->add($item);
|
$this->getAll()->add($item);
|
||||||
$this->added($item);
|
$this->added($item);
|
||||||
}
|
}
|
||||||
|
@@ -101,6 +101,9 @@ abstract class WireSaveableItemsLookup extends WireSaveableItems {
|
|||||||
*
|
*
|
||||||
* Template method used by ___save()
|
* Template method used by ___save()
|
||||||
*
|
*
|
||||||
|
* @param string $key
|
||||||
|
* @return bool
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
protected function saveItemKey($key) {
|
protected function saveItemKey($key) {
|
||||||
if($key == $this->getLookupField()) return false;
|
if($key == $this->getLookupField()) return false;
|
||||||
@@ -117,7 +120,10 @@ abstract class WireSaveableItemsLookup extends WireSaveableItems {
|
|||||||
*/
|
*/
|
||||||
public function ___save(Saveable $item) {
|
public function ___save(Saveable $item) {
|
||||||
|
|
||||||
if(!$item instanceof HasLookupItems) throw new WireException($this->className() . "::save() requires an item that implements HasLookupItems interface");
|
if(!$item instanceof HasLookupItems) {
|
||||||
|
$class = $this->className();
|
||||||
|
throw new WireException("$class::save() requires an item that implements HasLookupItems interface");
|
||||||
|
}
|
||||||
|
|
||||||
$database = $this->wire('database');
|
$database = $this->wire('database');
|
||||||
$lookupTable = $database->escapeTable($this->getLookupTable());
|
$lookupTable = $database->escapeTable($this->getLookupTable());
|
||||||
|
Reference in New Issue
Block a user