private function setDataForItem($itemId, $tags, $thisExisting)
    {
        if ($tags === null) {
            $tagIds = [];
        } else {
            $tagIds = \array_keys($tags);
        }

        // First pass, delete all not mentioned anymore.
        $valuesToRemove = \array_diff($thisExisting, $tagIds);
        if ($valuesToRemove) {
            $this->connection
                ->createQueryBuilder()
                ->delete('tl_metamodel_tag_relation')
                ->where('att_id=:attId')
                ->andWhere('item_id=:itemId')
                ->andWhere('value_id IN (:valueIds)')
                ->setParameter('attId', $this->get('id'))
                ->setParameter('itemId', $itemId)
                ->setParameter('valueIds', $valuesToRemove, Connection::PARAM_STR_ARRAY)
                ->execute();
        }

        // Second pass, add all new values in a row.
        $valuesToAdd  = \array_diff($tagIds, $thisExisting);
        $insertValues = [];
        if ($valuesToAdd) {
            foreach ($valuesToAdd as $valueId) {
                $insertValues[] = [
                    'attId'   => $this->get('id'),
                    'itemId'  => $itemId,
                    'sorting' => (int) $tags[$valueId]['tag_value_sorting'],
                    'valueId' => $valueId
                ];
            }
        }

        // Third pass, update all sorting values.
        $valuesToUpdate = \array_diff($tagIds, $valuesToAdd);
        if ($valuesToUpdate) {
            $query = $this->connection
                ->createQueryBuilder()
                ->update('tl_metamodel_tag_relation')
                ->set('value_sorting', ':sorting')
                ->where('att_id=:attId')
                ->andWhere('item_id=:itemId')
                ->andWhere('value_id=:valueId')
                ->setParameter('attId', $this->get('id'))
                ->setParameter('itemId', $itemId);

            foreach ($valuesToUpdate as $valueId) {
                if (!array_key_exists('tag_value_sorting', $tags[$valueId])) {
                    continue;
                }
                $query
                    ->setParameter('sorting', (int) $tags[$valueId]['tag_value_sorting'])
                    ->setParameter('valueId', $valueId)
                    ->execute();
            }
        }

        return $insertValues;
    }