Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX : ref should be UNIQUE for dolibarr objects #32423

Closed
wants to merge 4 commits into from
Closed
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
9 changes: 9 additions & 0 deletions htdocs/core/class/commonobject.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
*
* @phan-forbid-undeclared-magic-properties
*/
abstract class CommonObject

Check warning on line 50 in htdocs/core/class/commonobject.class.php

View workflow job for this annotation

GitHub Actions / pre-commit / pre-commit

Possible parse error: class missing opening or closing brace

Check warning on line 50 in htdocs/core/class/commonobject.class.php

View workflow job for this annotation

GitHub Actions / pre-commit / pre-commit

Possible parse error: class missing opening or closing brace
{
use DolDeprecationHandler;

Expand Down Expand Up @@ -875,7 +875,7 @@
*
* @return array<string,string>
*/
protected function deprecatedProperties()

Check failure on line 878 in htdocs/core/class/commonobject.class.php

View workflow job for this annotation

GitHub Actions / pre-commit / pre-commit

Line indented incorrectly; expected 0 tabs, found 1
{
return array(
'alreadypaid' => 'totalpaid',
Expand All @@ -888,7 +888,7 @@
'projet' => 'project',
'statut' => 'status',
);
}

Check failure on line 891 in htdocs/core/class/commonobject.class.php

View workflow job for this annotation

GitHub Actions / pre-commit / pre-commit

Line indented incorrectly; expected 0 tabs, found 1


/**
Expand All @@ -901,7 +901,7 @@
* @param string $ref_ext Ref ext of object to check
* @return int Return integer <0 if KO, 0 if OK but not found, >0 if OK and exists
*/
public static function isExistingObject($element, $id, $ref = '', $ref_ext = '')

Check failure on line 904 in htdocs/core/class/commonobject.class.php

View workflow job for this annotation

GitHub Actions / pre-commit / pre-commit

Line indented incorrectly; expected 0 tabs, found 1
{
global $db, $conf;

Expand All @@ -909,20 +909,20 @@
$sql .= " FROM ".$db->prefix().$element;
$sql .= " WHERE entity IN (".getEntity($element).")";

if ($id > 0) {

Check failure on line 912 in htdocs/core/class/commonobject.class.php

View workflow job for this annotation

GitHub Actions / pre-commit / pre-commit

Line indented incorrectly; expected 1 tabs, found 2
$sql .= " AND rowid = ".((int) $id);
} elseif ($ref) {

Check failure on line 914 in htdocs/core/class/commonobject.class.php

View workflow job for this annotation

GitHub Actions / pre-commit / pre-commit

Line indented incorrectly; expected 1 tabs, found 2
$sql .= " AND ref = '".$db->escape($ref)."'";
} elseif ($ref_ext) {

Check failure on line 916 in htdocs/core/class/commonobject.class.php

View workflow job for this annotation

GitHub Actions / pre-commit / pre-commit

Line indented incorrectly; expected 1 tabs, found 2
$sql .= " AND ref_ext = '".$db->escape($ref_ext)."'";
} else {

Check failure on line 918 in htdocs/core/class/commonobject.class.php

View workflow job for this annotation

GitHub Actions / pre-commit / pre-commit

Line indented incorrectly; expected 1 tabs, found 2
$error = 'ErrorWrongParameters';
dol_syslog(get_class()."::isExistingObject ".$error, LOG_ERR);
return -1;
}

Check failure on line 922 in htdocs/core/class/commonobject.class.php

View workflow job for this annotation

GitHub Actions / pre-commit / pre-commit

Line indented incorrectly; expected 1 tabs, found 2
if ($ref || $ref_ext) { // Because the same ref can exists in 2 different entities, we force the current one in priority

Check failure on line 923 in htdocs/core/class/commonobject.class.php

View workflow job for this annotation

GitHub Actions / pre-commit / pre-commit

Line indented incorrectly; expected 1 tabs, found 2
$sql .= " AND entity = ".((int) $conf->entity);
}

Check failure on line 925 in htdocs/core/class/commonobject.class.php

View workflow job for this annotation

GitHub Actions / pre-commit / pre-commit

Line indented incorrectly; expected 1 tabs, found 2

dol_syslog(get_class()."::isExistingObject", LOG_DEBUG);
$resql = $db->query($sql);
Expand Down Expand Up @@ -10378,6 +10378,15 @@
}
$key_fields = $this->fields[$key];

// Check ref
if ($key == 'ref' && !empty($values['ref'])) { // We check that ref is not already used
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely a bug here: We add a { and we add another one line 10384 but there is only one closing added.

$result = self::isExistingObject($this->element, 0, $values['ref']); // Check ref is not yet used
if ($result > 0) {
$error++;
$langs->load("errors");
$this->errors[] = $langs->trans("ErrorRefAlreadyExists");
}

// If field is an implicit foreign key field (so type = 'integer:...')
if (preg_match('/^integer:/i', $key_fields['type']) && $values[$key] == '-1') {
$values[$key] = '';
Expand Down
2 changes: 1 addition & 1 deletion htdocs/core/lib/modulebuilder.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ function rebuildObjectSql($destdir, $module, $objectname, $newmask, $readdir = '
foreach ($object->fields as $key => $val) {
$i++;
if (!empty($val['index'])) {
$texttoinsert .= "ALTER TABLE llx_".strtolower($module).'_'.strtolower($objectname)." ADD INDEX idx_".strtolower($module).'_'.strtolower($objectname)."_".$key." (".$key.");";
$texttoinsert .= "ALTER TABLE llx_" . strtolower($module) . '_' . strtolower($objectname) . " ADD " . ($key == 'ref' ? "UNIQUE INDEX uk_" : "INDEX idx_") . strtolower($module) . '_' . strtolower($objectname) . "_" . $key . " (" . $key . ($key == 'ref' && array_key_exists('entity', $object->fields) ? ", entity" : "") . ");";
$texttoinsert .= "\n";
}
if (!empty($val['foreignkey'])) {
Expand Down
Loading