Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions inc/container.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 [];
}

Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions inc/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
],
Expand Down
60 changes: 60 additions & 0 deletions tests/Units/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
Loading