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

NEW: API setup update extrafields from name, elementtype and json #29273

Merged
merged 15 commits into from
Apr 10, 2024

Conversation

JonBendtsen
Copy link
Contributor

@JonBendtsen JonBendtsen commented Apr 7, 2024

NEW setup API can update new extrafields
Third step in fixing #29249 though this pull request can only update.

@JonBendtsen
Copy link
Contributor Author

This is the json I've tested it with, and it ignores the fields in the json that is not specified in the function

	public function updateExtraField($attrname, $label, $type, $pos, $size, $elementtype, $unique = 0, $required = 0, $default_value = '', $param = '', $alwayseditable = 0, $perms = '', $list = '-1', $help = '', $computed = '', $entity = '', $langfile = '', $enabled = '1', $totalizable = 0, $printable = 0, $moreparams = array())
{       "id": "5",
      "type": "varchar",
      "label": "schone",
      "size": "255",
      "elementtype": "commande",
      "default": null,
      "computed": null,
      "unique": "1",
      "required": "0",
      "param": {
        "options": {
          "": null
        }
      },
      "pos": "107",
      "alwayseditable": "1",
      "perms": null,
      "list": "1",
      "entity": "1",
      "printable": "1",
      "totalizable": "1",
      "langs": null,
      "help": "test, bare en simpel test",
      "css": null,
      "cssview": null,
      "csslist": null,
      "fk_user_author": "2",
      "fk_user_modif": "3"
    }

@JonBendtsen
Copy link
Contributor Author

JonBendtsen commented Apr 7, 2024

request url http://localhost/api/index.php/setup/extrafields/commande/one

This what it looks like in my browser. I decided to use this setup because then it looks more like the delete, and I think it fits better with the style that I see when using the existing method GET extrafields.

API_setup_update_extrafield

@JonBendtsen JonBendtsen marked this pull request as ready for review April 7, 2024 21:39
@eldy eldy added the PR to fix - See feedback in comments PR needs to be fixed to be integrated (some comments should describes the fix to do) label Apr 8, 2024
@JonBendtsen
Copy link
Contributor Author

@eldy I think that the conflicts are fixed now, as for the POST I would like to re-make it in the style of delete and update, meaning attrname and elementtype in the url, the rest in json in the request_data

Copy link
Member

@eldy eldy left a comment

Choose a reason for hiding this comment

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

For name of column or tables db->sanitize is better then db->escape

htdocs/core/class/extrafields.class.php Show resolved Hide resolved
@JonBendtsen
Copy link
Contributor Author

@eldy should be sanitized now, and that one phan error I do not understand because it is not in a file I changed?

@hregis
Copy link
Contributor

hregis commented Apr 8, 2024

@JonBendtsen replace just

"* @param string $useempty....."

with

"* @param int $useempty...."

@JonBendtsen
Copy link
Contributor Author

@hregis for the phan error in a file I did not change?

@hregis
Copy link
Contributor

hregis commented Apr 8, 2024

@JonBendtsen phan check is completely alien to me! I don't know how it decides to check a file at one time and not at another... ;-)

#29283

@hregis
Copy link
Contributor

hregis commented Apr 8, 2024

@JonBendtsen maybe @mdeweerd can answer this question ! ;-)

@JonBendtsen
Copy link
Contributor Author

At the time of posting this comment the contents of the changes to allow for updating has been copied to #29270 where there is also the additional creation of extra fields.

@hregis
Copy link
Contributor

hregis commented Apr 8, 2024

@JonBendtsen @eldy just waiting integration of #29283 and after merge develop branch in your PR branch

@JonBendtsen
Copy link
Contributor Author

JonBendtsen commented Apr 8, 2024

@JonBendtsen @eldy just waiting integration of #29283 and after merge develop branch in your PR branch

@hregis okay, I'll wait for #29283 too and then merge develop into my branch.

(Do you guys have a chat where you're hanging out since you have that kind of information?)

@hregis
Copy link
Contributor

hregis commented Apr 8, 2024

@JonBendtsen there must be a conversation channel but I don't remember which one... I'm old school and all this is beyond me and I don't have the time to take the time anymore... but I can help you on github if you put me in the "@hregis" dialog

@JonBendtsen
Copy link
Contributor Author

@JonBendtsen there must be a conversation channel but I don't remember which one... I'm old school and all this is beyond me and I don't have the time to take the time anymore... but I can help you on github if you put me in the "@hregis" dialog

@hregis it is probably in French which I don't understand, so it would probably not be that helpful for me

@mdeweerd
Copy link
Contributor

mdeweerd commented Apr 8, 2024

@JonBendtsen phan check is completely alien to me! I don't know how it decides to check a file at one time and not at another... ;-)

#29283

A far as I see the integration of the commit 3d2a018 brought the following phpdoc that I think is now fixed.

	 * @param	string				$useempty		1=Add an empty value in list, 2=Add an empty value in list only if there is more than 2 entries.

So I probably was invalid the develop branch as well. In that case the issue in the develop branch propagates to the PR.

@eldy eldy merged commit f7eb0b9 into Dolibarr:develop Apr 10, 2024
7 checks passed
@JonBendtsen JonBendtsen deleted the update_ExtrafieldsFromNameElement branch April 10, 2024 11:03
@MarcVJ
Copy link

MarcVJ commented Jan 28, 2025

Hello,
I noticed an error on this SQL query : the syntax "IF EXISTS" is not valid in MySQL, this error pops in my log :
"DoliDBMysqli::query SQL Error message: DB_ERROR_SYNTAX You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF EXISTS uk_product_extrafields_prixaukgoulitre' at line 1"

I did a grep on the entire dolibarr-20.0.2 file and the only line I found containing "IF EXISTS" is this one in htdocs/core/class/extrafields.class.php :
$sql = "ALTER TABLE ".$this->db->prefix().$table." DROP INDEX IF EXISTS uk_".$table."_".$attrname;

@mdeweerd
Copy link
Contributor

htdocs/core/class/extrafields.class.php:776:69:                      $sql = "ALTER TABLE ".$this->db->prefix().$table." DROP INDEX IF EXISTS uk_".$table."_".$this->db->sanitize($attrname);
htdocs/core/class/utils.class.php:1195:33:               fwrite($handle, "DROP TABLE IF EXISTS `".$table."`;\n"); // Dropping table if exists prior to re create it
htdocs/core/class/utils.class.php:1262:35:        fwrite($handle,"DROP PROCEDURE IF EXISTS '$name'.'$proc'$$\n");

When I look at the code for the first occurence which matches your error message, the "DROP INDEX" is supposed to undo the create index - however the "ALTER TABLE ".$this->db->prefix().$table. is incorrect. The index name is unique itself, and "DROP INDEX" does not use "ALTER TABLE". The 'IF EXISTS' seems correct.

@JonBendtsen
Copy link
Contributor Author

JonBendtsen commented Jan 28, 2025

@MarcVJ do you get a different result if you edit the line a little?

from:
$sql = "ALTER TABLE ".$this->db->prefix().$table." DROP INDEX IF EXISTS uk_".$table."_".$attrname;

to:
$sql = "ALTER TABLE ".$this->db->prefix().$table." DROP INDEX IF EXISTS uk_".$table."_".$this->db->sanitize($attrname);

@mdeweerd
Copy link
Contributor

I think it has to be:

$sql = "DROP INDEX IF EXISTS uk_".$table."_".$attrname;

@JonBendtsen
Copy link
Contributor Author

@mdeweerd don't forget .$this->db->sanitize($attrname);

@MarcVJ
Copy link

MarcVJ commented Jan 29, 2025

@JonBendtsen I tried with the edited line :
$sql = "ALTER TABLE ".$this->db->prefix().$table." DROP INDEX IF EXISTS uk_".$table."_".$this->db->sanitize($attrname);
I didn't get the same error message, I created an extra field (with the Product module), tried to create another with the same name, then suppressed it. I got this in the logs :

2025-01-29 08:17:16 ERR     2a01:cb04:6d2:e00:b5c2:233c:cf81:9977 DoliDBMysqli::query SQL Error query: ALTER TABLE llx_product_extrafields ADD test2 varchar(255) NULL
2025-01-29 08:17:16 ERR     2a01:cb04:6d2:e00:b5c2:233c:cf81:9977 DoliDBMysqli::query SQL Error message: DB_ERROR_COLUMN_ALREADY_EXISTS Duplicate column name 'test2'
2025-01-29 08:17:16 ERR     2a01:cb04:6d2:e00:b5c2:233c:cf81:9977 DoliDBMysqli::query SQL Error query: INSERT INTO llx_extrafields( name, label, type, pos, size, entity, elementtype, fieldunique, fieldrequired, param, alwayseditable, perms, langs, list, printable, fielddefault, fieldcomputed, fk_user_author, fk_user_modif, datec, enabled, help, totalizable, css, csslist, cssview ) VALUES('test2', 'Test 2', 'varchar', 100, '255', 1, 'product', 0, 0, 'a:1:{s:7:\"options\";a:1:{s:0:\"\";N;}}', 1, null, null, '1', '0', null, null, 25, 25,'2025-01-29 09:17:16', '1', null, FALSE, null, null, null)
2025-01-29 08:17:16 ERR     2a01:cb04:6d2:e00:b5c2:233c:cf81:9977 DoliDBMysqli::query SQL Error message: DB_ERROR_RECORD_ALREADY_EXISTS Duplicate entry 'test2-1-product' for key 'llx_extrafields.uk_extrafields_name'

@MarcVJ
Copy link

MarcVJ commented Jan 29, 2025

@mdeweerd I also tried :
$sql = "DROP INDEX IF EXISTS uk_".$table."_".$this->db->sanitize($attrname);
An got those logs :

025-01-29 08:25:08 ERR     2a01:cb04:6d2:e00:b5c2:233c:cf81:9977 DoliDBMysqli::query SQL Error query: ALTER TABLE llx_product_extrafields ADD test2 varchar(255) NULL
2025-01-29 08:25:08 ERR     2a01:cb04:6d2:e00:b5c2:233c:cf81:9977 DoliDBMysqli::query SQL Error message: DB_ERROR_COLUMN_ALREADY_EXISTS Duplicate column name 'test2'
2025-01-29 08:25:08 ERR     2a01:cb04:6d2:e00:b5c2:233c:cf81:9977 DoliDBMysqli::query SQL Error query: INSERT INTO llx_extrafields( name, label, type, pos, size, entity, elementtype, fieldunique, fieldrequired, param, alwayseditable, perms, langs, list, printable, fielddefault, fieldcomputed, fk_user_author, fk_user_modif, datec, enabled, help, totalizable, css, csslist, cssview ) VALUES('test2', 'Test 2', 'varchar', 100, '255', 1, 'product', 0, 0, 'a:1:{s:7:\"options\";a:1:{s:0:\"\";N;}}', 1, null, null, '1', '0', null, null, 25, 25,'2025-01-29 09:25:08', '1', null, FALSE, null, null, null)
2025-01-29 08:25:08 ERR     2a01:cb04:6d2:e00:b5c2:233c:cf81:9977 DoliDBMysqli::query SQL Error message: DB_ERROR_RECORD_ALREADY_EXISTS Duplicate entry 'test2-1-product' for key 'llx_extrafields.uk_extrafields_name'
2025-01-29 08:25:31 ERR     2a01:cb04:6d2:e00:b5c2:233c:cf81:9977 DoliDBMysqli::query SQL Error query: DROP INDEX IF EXISTS uk_product_extrafields_test3
2025-01-29 08:25:31 ERR     2a01:cb04:6d2:e00:b5c2:233c:cf81:9977 DoliDBMysqli::query SQL Error message: DB_ERROR_SYNTAX You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF EXISTS uk_product_extrafields_test3' at line 1

@hregis
Copy link
Contributor

hregis commented Jan 29, 2025

@JonBendtsen @MarcVJ it doesn't exists ! DROP INDEX IF EXISTS

@hregis
Copy link
Contributor

hregis commented Jan 29, 2025

@hregis
Copy link
Contributor

hregis commented Jan 29, 2025

i send a fix : #32848

@mdeweerd
Copy link
Contributor

This resource suggests that DROP INDEX IF EXISTS index_name ON table_name; exists.
https://www.tutorialspoint.com/sql/sql-drop-index.htm#drop_index_if_exists

However I did not look in detail and therefore the command should rather ba:

$sql = "DROP INDEX IF EXISTS uk_".$table."_".$this->db->sanitize($attrname)." ON ".$this->db->prefix().$table;

Maybe that does not exist for the flavor (the error was a syntax error without the ' ON ').

@hregis
Copy link
Contributor

hregis commented Jan 29, 2025

@mdeweerd ok for postgresql and maybe for SQL server, but not for Mysql/MariaDB !

@hregis
Copy link
Contributor

hregis commented Jan 29, 2025

@hregis
Copy link
Contributor

hregis commented Jan 29, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR to fix - See feedback in comments PR needs to be fixed to be integrated (some comments should describes the fix to do)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants