mirror of
https://github.com/processwire/processwire.git
synced 2025-08-16 11:44:42 +02:00
Fix issue processwire/processwire-issues#598 in FieldtypeOptions where manually assigned option IDs were not working
This commit is contained in:
@@ -18,7 +18,7 @@
|
|||||||
*
|
*
|
||||||
* @property array $where
|
* @property array $where
|
||||||
* @property array $bindValues
|
* @property array $bindValues
|
||||||
* @properety array $bindIndex
|
* @property array $bindIndex
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
abstract class DatabaseQuery extends WireData {
|
abstract class DatabaseQuery extends WireData {
|
||||||
@@ -216,6 +216,9 @@ abstract class DatabaseQuery extends WireData {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute the query with the current database handle
|
* Execute the query with the current database handle
|
||||||
|
*
|
||||||
|
* @return \PDOStatement
|
||||||
|
* @throws WireException|\Exception|\PDOException
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public function execute() {
|
public function execute() {
|
||||||
|
@@ -155,6 +155,8 @@ class SelectableOptionManager extends Wire {
|
|||||||
$sorted[] = $option;
|
$sorted[] = $option;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$query->closeCursor();
|
||||||
|
|
||||||
$options = $this->wire(new SelectableOptionArray());
|
$options = $this->wire(new SelectableOptionArray());
|
||||||
$options->setField($field);
|
$options->setField($field);
|
||||||
@@ -183,17 +185,20 @@ class SelectableOptionManager extends Wire {
|
|||||||
// no need to use fulltext matching if operator is not a partial match operator
|
// no need to use fulltext matching if operator is not a partial match operator
|
||||||
return $this->getOptions($field, array($property => $value));
|
return $this->getOptions($field, array($property => $value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @var DatabaseQuerySelect $query */
|
||||||
$query = $this->wire(new DatabaseQuerySelect());
|
$query = $this->wire(new DatabaseQuerySelect());
|
||||||
$query->select('*');
|
$query->select('*');
|
||||||
$query->from(self::optionsTable);
|
$query->from(self::optionsTable);
|
||||||
$query->where("fields_id=:fields_id");
|
$query->where("fields_id=:fields_id");
|
||||||
$query->bindValue(':fields_id', $field->id);
|
$query->bindValue(':fields_id', $field->id);
|
||||||
|
|
||||||
|
/** @var DatabaseQuerySelectFulltext $ft */
|
||||||
$ft = $this->wire(new DatabaseQuerySelectFulltext($query));
|
$ft = $this->wire(new DatabaseQuerySelectFulltext($query));
|
||||||
$ft->match(self::optionsTable, $property, $operator, $value);
|
$ft->match(self::optionsTable, $property, $operator, $value);
|
||||||
|
|
||||||
$result = $query->execute();
|
$result = $query->execute();
|
||||||
|
/** @var SelectableOptionArray $options */
|
||||||
$options = $this->wire(new SelectableOptionArray());
|
$options = $this->wire(new SelectableOptionArray());
|
||||||
$options->setField($field);
|
$options->setField($field);
|
||||||
|
|
||||||
@@ -642,18 +647,35 @@ class SelectableOptionManager extends Wire {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public function addOptions(Field $field, $options) {
|
public function addOptions(Field $field, $options) {
|
||||||
|
|
||||||
|
/** @var WireDatabasePDO $database */
|
||||||
$database = $this->wire('database');
|
$database = $this->wire('database');
|
||||||
|
|
||||||
|
// options that have pre-assigned IDs
|
||||||
|
$optionsByID = array();
|
||||||
|
|
||||||
$sql =
|
// determine if any added options already have IDs
|
||||||
"SELECT MAX(option_id) FROM " . self::optionsTable . " " .
|
foreach($options as $option) {
|
||||||
"WHERE fields_id=:fields_id";
|
if(!$option instanceof SelectableOption || !strlen($option->title)) continue;
|
||||||
|
if($option->id > 0) $optionsByID[(int) $option->id] = $option;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(count($options) > count($optionsByID)) {
|
||||||
|
// Determine starting value (max) for auto-assigned IDs
|
||||||
|
$sql =
|
||||||
|
"SELECT MAX(option_id) FROM " . self::optionsTable . " " .
|
||||||
|
"WHERE fields_id=:fields_id";
|
||||||
|
|
||||||
$query = $database->prepare($sql);
|
$query = $database->prepare($sql);
|
||||||
$query->bindValue(':fields_id', $field->id);
|
$query->bindValue(':fields_id', $field->id);
|
||||||
$query->execute();
|
$query->execute();
|
||||||
|
|
||||||
list($max) = $query->fetch(\PDO::FETCH_NUM);
|
list($max) = $query->fetch(\PDO::FETCH_NUM);
|
||||||
|
$query->closeCursor();
|
||||||
|
} else {
|
||||||
|
// there are no auto-assigned IDs
|
||||||
|
$max = 0;
|
||||||
|
}
|
||||||
|
|
||||||
$sql =
|
$sql =
|
||||||
"INSERT INTO " . self::optionsTable . " " .
|
"INSERT INTO " . self::optionsTable . " " .
|
||||||
@@ -664,12 +686,16 @@ class SelectableOptionManager extends Wire {
|
|||||||
$query = $database->prepare($sql);
|
$query = $database->prepare($sql);
|
||||||
|
|
||||||
foreach($options as $option) {
|
foreach($options as $option) {
|
||||||
if(!strlen($option->title)) continue;
|
if(!$option instanceof SelectableOption || !strlen($option->title)) continue;
|
||||||
if(!$option instanceof SelectableOption) continue;
|
if($option->id > 0) {
|
||||||
$id = ++$max;
|
$id = $option->id;
|
||||||
$query->bindValue(':fields_id', $field->id);
|
} else {
|
||||||
$query->bindValue(':option_id', $id);
|
$id = ++$max;
|
||||||
$query->bindValue(':sort', $option->sort);
|
while(isset($optionsByID[$id])) $id++;
|
||||||
|
}
|
||||||
|
$query->bindValue(':fields_id', $field->id, \PDO::PARAM_INT);
|
||||||
|
$query->bindValue(':option_id', $id, \PDO::PARAM_INT);
|
||||||
|
$query->bindValue(':sort', $option->sort, \PDO::PARAM_INT);
|
||||||
$query->bindValue(':title', $option->title);
|
$query->bindValue(':title', $option->title);
|
||||||
$query->bindValue(':value', $option->value);
|
$query->bindValue(':value', $option->value);
|
||||||
|
|
||||||
@@ -696,7 +722,7 @@ class SelectableOptionManager extends Wire {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public function updateLanguages(HookEvent $event = null) {
|
public function updateLanguages(HookEvent $event = null) {
|
||||||
|
if($event) {} // ignore
|
||||||
if(!$this->useLanguages) return;
|
if(!$this->useLanguages) return;
|
||||||
|
|
||||||
$database = $this->wire('database');
|
$database = $this->wire('database');
|
||||||
|
Reference in New Issue
Block a user