diff --git a/CHANGELOG.md b/CHANGELOG.md index d63779fa..c92bf71a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Remove redundant variable initialization in the question type AJAX controller - Fix `Some mandatory fields are empty` error displayed when creating an item with an assigned technician +<<<<<<< HEAD - Fix item creation failing when a Yes/No field has a non boolean default value +======= +- Fix slow display and save of items with additional fields when the values tables are large +- Fix slow page loads when many entities are active: the containers available to the user were computed with one query per container on every request +- Fix item clone failing when a mandatory dropdown field has an empty default value +>>>>>>> 449f576 (Fix: use itemtype in values lookups (#1312)) - Fix search on number fields: `is` / `is not` ignoring values from 1000, `<` / `>` comparing values as text, and `is not` excluding items without value - Fix GenericObject migration error message to name the required 3.0.x version diff --git a/inc/container.class.php b/inc/container.class.php index dd4c7559..33031ea5 100644 --- a/inc/container.class.php +++ b/inc/container.class.php @@ -1055,7 +1055,7 @@ public static function preItemPurge($item) } $fields = new $classname(); - $fields->deleteByCriteria(['items_id' => $item->fields['id']], true); + $fields->deleteByCriteria(['itemtype' => $itemtype, 'items_id' => $item->fields['id']], true); } } @@ -1775,7 +1775,7 @@ public function updateFieldsValues($data, $itemtype, $massiveaction = false) $obj = new $classname(); - $exist = $obj->getFromDBByCrit(['items_id' => $items_id]); + $exist = $obj->getFromDBByCrit(['itemtype' => $itemtype, 'items_id' => $items_id]); // Convert "multiple" values into a JSON string $multiple_fields_iterator = $DB->request([ @@ -1820,7 +1820,7 @@ public function updateFieldsValues($data, $itemtype, $massiveaction = false) $obj = $dbu->getItemForItemtype($classname); if ($obj !== false) { - if ($obj->getFromDBByCrit(['items_id' => $items_id]) === false) { + if ($obj->getFromDBByCrit(['itemtype' => $itemtype, 'items_id' => $items_id]) === false) { // add fields data $obj->add($data); } else { @@ -2270,7 +2270,7 @@ private static function getStoredValues(self $container, string $itemtype, int $ } $values_obj = (new DbUtils())->getItemForItemtype(self::getClassname($itemtype, $container->fields['name'])); - if ($values_obj === false || !$values_obj->getFromDBByCrit(['items_id' => $items_id])) { + if ($values_obj === false || !$values_obj->getFromDBByCrit(['itemtype' => $itemtype, 'items_id' => $items_id])) { return []; } @@ -2712,7 +2712,7 @@ private static function checkContainerMandatory(CommonDBTM $item, PluginFieldsCo $classname = self::getClassname($item::class, $loc_c->fields['name']); $dbu = new DbUtils(); $obj = $dbu->getItemForItemtype($classname); - if ($obj !== false && $obj->getFromDBByCrit(['items_id' => $item->getID()])) { + if ($obj !== false && $obj->getFromDBByCrit(['itemtype' => $item::getType(), 'items_id' => $item->getID()])) { $data += $obj->fields; } else { $data += self::getPendingValues($item, $c_id); diff --git a/inc/field.class.php b/inc/field.class.php index 5b260692..68dfeb8a 100644 --- a/inc/field.class.php +++ b/inc/field.class.php @@ -1280,6 +1280,7 @@ public static function prepareHtmlFields( if ($obj instanceof CommonDBTM) { $found_values = $obj->find( [ + 'itemtype' => $item::getType(), 'plugin_fields_containers_id' => $first_field['plugin_fields_containers_id'], 'items_id' => $item->getID(), ], diff --git a/tests/Units/ContainerTest.php b/tests/Units/ContainerTest.php index 321b527f..de951bbc 100644 --- a/tests/Units/ContainerTest.php +++ b/tests/Units/ContainerTest.php @@ -1120,4 +1120,64 @@ public function testRenameOversizedContainerFailsOnNameCollision(): void $this->assertTrue($reloaded->getFromDB($container->getID())); $this->assertSame(0, (int) $reloaded->fields['is_active']); } + + public function testValuesAreReadAndPurgedForTheirItemtypeOnly(): void + { + $container = $this->createFieldContainer([ + 'label' => 'Itemtype values ' . $this->getUniqueString(), + 'type' => 'tab', + 'itemtypes' => [Computer::class], + 'is_active' => 1, + 'entities_id' => 0, + 'is_recursive' => 1, + ]); + + $field = $this->createField([ + 'label' => 'Itemtype text ' . str_replace('-', '', $this->getUniqueString()), + 'type' => 'text', + PluginFieldsContainer::getForeignKeyField() => $container->getID(), + 'ranking' => 1, + 'is_active' => 1, + 'is_readonly' => 0, + ]); + $field_name = $field->fields['name']; + + $computer = $this->createItem(Computer::class, [ + 'name' => 'Computer with itemtype values', + 'entities_id' => 0, + ]); + $this->assertTrue((new Computer())->update([ + 'id' => $computer->getID(), + $field_name => 'computer value', + ])); + + // Values row of another itemtype sharing the same items_id + /** @var DBmysql $DB */ + global $DB; + $table = getTableForItemType(PluginFieldsContainer::getClassname(Computer::class, $container->fields['name'])); + $DB->insert($table, [ + 'items_id' => $computer->getID(), + 'itemtype' => Monitor::class, + PluginFieldsContainer::getForeignKeyField() => $container->getID(), + $field_name => 'monitor value', + ]); + + $this->assertTrue((new Computer())->update([ + 'id' => $computer->getID(), + $field_name => 'updated computer value', + ])); + + $values = []; + foreach ($DB->request(['FROM' => $table, 'WHERE' => ['items_id' => $computer->getID()]]) as $row) { + $values[$row['itemtype']] = $row[$field_name]; + } + + $this->assertSame([Computer::class => 'updated computer value', Monitor::class => 'monitor value'], $values); + + $this->assertTrue((new Computer())->delete(['id' => $computer->getID()], true)); + + $rows = $DB->request(['FROM' => $table, 'WHERE' => ['items_id' => $computer->getID()]]); + $this->assertCount(1, $rows); + $this->assertSame(Monitor::class, $rows->current()['itemtype']); + } }