1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-12 09:44:38 +02:00

Improvements to FieldtypeMulti::savePageFieldRows method

This commit is contained in:
Ryan Cramer
2021-05-05 13:57:11 -04:00
parent 8d9a66fc38
commit 644f0fb3fd

View File

@@ -657,18 +657,17 @@ abstract class FieldtypeMulti extends Fieldtype {
if(count($primaryKeys) !== 1) throw new WireException("savePageFieldRows() can only be used on fieldtypes with 1 primary key"); if(count($primaryKeys) !== 1) throw new WireException("savePageFieldRows() can only be used on fieldtypes with 1 primary key");
$value = $this->setupPageFieldRows($page, $field, $value); $value = $this->setupPageFieldRows($page, $field, $value);
$database = $this->wire('database'); $database = $this->wire()->database;
$table = $database->escapeTable($info['table']); $table = $database->escapeTable($info['table']);
$primaryKey = $database->escapeCol(reset($primaryKeys)); $primaryKey = $database->escapeCol(reset($primaryKeys));
$hasInserts = false; $hasInserts = false;
$sort = null; $sort = null;
$numSaved = 0; $numSaved = 0;
$locked = false;
// sleep the values for storage // sleep the values for storage
$sleepValue = $this->sleepValue($page, $field, $value); $sleepValue = $this->sleepValue($page, $field, $value);
$this->lockForWriting($field);
if(isset($schema['sort'])) { if(isset($schema['sort'])) {
// determine if there are any INSERTs and what the next sort value(s) should be // determine if there are any INSERTs and what the next sort value(s) should be
// this is because "pages_id,sort" are generally a unique index with FieldtypeMulti // this is because "pages_id,sort" are generally a unique index with FieldtypeMulti
@@ -680,6 +679,8 @@ abstract class FieldtypeMulti extends Fieldtype {
if(isset($v['sort']) && $v['sort'] > $maxSort) $maxSort = $v['sort']; if(isset($v['sort']) && $v['sort'] > $maxSort) $maxSort = $v['sort'];
} }
if($hasInserts) { if($hasInserts) {
// we will need a locked table for inserts
if(!$locked) $locked = $this->lockForWriting($field);
// determine max sort value for new items inserted // determine max sort value for new items inserted
$sort = $this->getMaxColumnValue($page, $field, 'sort', -1); $sort = $this->getMaxColumnValue($page, $field, 'sort', -1);
if($maxSort > $sort) $sort = $maxSort; if($maxSort > $sort) $sort = $maxSort;
@@ -724,11 +725,16 @@ abstract class FieldtypeMulti extends Fieldtype {
try { try {
if($query->execute()) $numSaved++; if($query->execute()) $numSaved++;
} catch(\Exception $e) { } catch(\Exception $e) {
$this->error($e->getMessage(), $this->wire('user')->isSuperuser() ? Notice::logOnly : Notice::log); $this->trackException($e, false);
if($this->wire()->user->isSuperuser()) {
$this->error($e->getMessage(), Notice::log);
} else {
$this->error($e->getMessage(), Notice::logOnly);
}
} }
} }
$this->unlockForWriting(); if($locked) $this->unlockForWriting();
return $numSaved; return $numSaved;
} }
@@ -741,18 +747,27 @@ abstract class FieldtypeMulti extends Fieldtype {
* *
*/ */
protected function lockForWriting(Field $field) { protected function lockForWriting(Field $field) {
$database = $this->wire('database');
$database = $this->wire()->database;
$table = $database->escapeTable($field->getTable()); $table = $database->escapeTable($field->getTable());
$locked = false; $locked = false;
try { $numAttempts = 0;
// attempt lock if possible $maxAttempts = 100;
if($database->exec("LOCK TABLES `$table` WRITE") !== false) { $lastException = null;
$this->lockedTable = true;
$locked = true; do {
try {
// attempt lock if possible
if($database->exec("LOCK TABLES `$table` WRITE") !== false) {
$this->lockedTable = true;
$locked = true;
}
} catch(\Exception $e) {
$lastException = $e;
} }
} catch(\Exception $e) { } while(!$locked && $numAttempts++ < $maxAttempts);
// ignore
} if(!$locked && $lastException) $this->trackException($lastException, false);
return $locked; return $locked;
} }
@@ -766,11 +781,11 @@ abstract class FieldtypeMulti extends Fieldtype {
protected function unlockForWriting() { protected function unlockForWriting() {
$result = false; $result = false;
if($this->lockedTable) try { if($this->lockedTable) try {
$this->wire('database')->exec("UNLOCK TABLES"); $this->wire()->database->exec("UNLOCK TABLES");
$this->lockedTable = false; $this->lockedTable = false;
$result = true; $result = true;
} catch(\Exception $e) { } catch(\Exception $e) {
// ignore $this->trackException($e, false);
} }
return $result; return $result;
} }