Field as subquery with CONACT_WS renders wrong
Field as subquery with CONACT_WS renders wrong
Error messages shown:
SQLSTATE[42000]: Syntax error or access violation: 1583 Incorrect parameters in the call to native function 'concat_ws
Description of problem:
I am trying to create a field by using a subquery where i want to group_concatenated two concatenated columns. The problem is that it seems that the library is rendering it wrongly.
This is the expected query which gives a correct result:
SELECT (SELECT GROUP_CONCAT(CONCAT_WS(": ", Name, Datum) ORDER BY tm_impfungentier.Datum SEPARATOR ", ") FROM tm_impfungentier JOIN tm_impfungen ON tm_impfungen.id = tm_impfungentier.Impfung WHERE Tier = tm_tiere.id) as Impfungen FROM tm_tiere
This is the query rendered by the library (returned by the editor in debug mode):
SELECT (SELECT GROUP_CONCAT(CONCAT_WS(": " as '(SELECT GROUP_CONCAT(CONCAT_WS(": "', Name
as 'Name', Datum) ORDER BY Datum) FROM tm_impfungentier JOIN tm_impfungen ON tm_impfungen.id = tm_impfungentier.Impfung WHERE Tier = tm_tiere.id) as 'Datum) ORDER BY Datum) FROM tm_impfungentier JOIN tm_impfungen ON tm_impfungen.id = tm_impfungentier.Impfung WHERE Tier = tm_tiere.id)' FROM tm_tiere
It is somehow duplicating the SELECT part of the subquery. Does anybody have a explanation for that?
Thanks!
Answers
Could you please post the code you use to generate that query,
Colin
Sure, how dumb to forget that sorry. Here is the field definition snippet of the editor:
@colin were you able to have a look at the issue? Thanks a lot!
Sorry - Colin asked me to take a look at it and I misplaced the tab!
What version of the Editor PHP libraries are you using?
I've just tried it, and code the following which is a little different from your own:
I don't have your database tables so I get an error, but I think that looks like it should work (if a very ugly alias!).
Allan
Hi @allan, thanks a lot for looking into this issue. Attached you'll find the versions i am using. I tried your definition but it did not work either. I dont really understand why you are aliasing like that.
Maybe you could double check if the version should work and then it would be great if you include the whole field::inst declaration into a snippet so i can try it out.
Thanks so much!
It is just using the string passed in as the value to read from the database. Otherwise I need to map it some other way - which is certainly possible, but this was the easiest option (although it might cause issues for more complex strings like this possibly).
Regarding the versions - that is the client-side versions. Could you have a look in the Editor.php file you have for the version string there?
Allan
The php library version is 1.6.1. I think it would help a lot if you could provide the whole Field instance definition.
Thanks a lot!
Yup - you'll want to update that .
You mean the one I used in my test? It was just the same as what you have above.
Allan
Hi @allan, thanks so much, updating the lib to the latest version fixed that issue. Now i have another little issue which is about date formatting:
Field::inst('(SELECT GROUP_CONCAT(CONCAT_WS(": ", Name, DATE_FORMAT(Datum, "%d.%m")) ORDER BY tm_impfungentier.Datum SEPARATOR "<br />") FROM tm_impfungentier JOIN tm_impfungen ON tm_impfungen.id = tm_impfungentier.Impfung WHERE Tier = tm_tiere.id GROUP BY tm_tiere.id)', 'Impfungen')
This problem is very weird, basically what i found out is: as long as i am using only two variables in the date format string it works, but if i want to add a third one (year) it returns null for the whole field.
I tested the same query directly with SQL and it worked so i suppose the problems lays in the lib. Can you confirm?
Thanks!
What is the JSON response from the server when you run it with three parameters please?
Allan
It is the normal valid response with proper data but that specific field is "null".
Sorry - I was actually after the
debug
information. Can you show me the full JSON please?Hi @allan, here you go. It is just the debug property because the other parts could contain sensitive information, sorry. If this is still not enough to pinpoint the problem i need to check it.
Thank you. I don't see any reason why adding the
%Y
would make any difference there. If you run:in a direct MySQL query (e.g. phpMyAdmin) what does happens?
I'm wondering if there is a size limit on the identifier and the extra two characters of %Y might just be taking it over that limit...
Allan
Running that in phpmyadmin works perfectly fine. So it must be something with datatables. I tried removing the two dots and only leaving "%d%m%Y" which should save the two characters %Y but it still does not work so i assume a size limit is not the reason.
I needed to extend the subquery a bit to actually select the correct data. After doing this i noticed that also those changes are causing datatables to return null for the column while in phpmyadmin the query works just fine:
(SELECT GROUP_CONCAT(impfungen) FROM (SELECT Tier, CONCAT_WS(": ", Name, MAX(Datum)) as impfungen FROM tm_impfungentier JOIN tm_impfungen ON tm_impfungen.id = tm_impfungentier.Impfung GROUP BY tm_impfungentier.Tier, tm_impfungentier.Impfung) t WHERE Tier = tm_tiere.id)
What happens if you use
%d.%Y
, or just%Y
? The query I pasted above is the one that the libraries are running, so if it works in phpMyAdmin, I can't see any reason why it wouldn't work in the library.I think I'd probably need a dump of your database schema and a full copy of the script to be able to debug what is going on here.
Thanks,
Allan
Hi @allan, regarding the second prolem (it is more important than the date): i tried now to debug myself and found out that MySQL is for some reason truncating the column name of the subquery in the result. That means that the Editor internal column name and the one in the mysql result are not matching anymore and that is why no data is delivered for that column.
I tried aliasing the subquery with
(SELECT GROUP_CONCAT(impfungen) FROM (SELECT Tier, CONCAT_WS(": ", Name, MAX(Datum)) as impfungen FROM tm_impfungentier JOIN tm_impfungen ON tm_impfungen.id = tm_impfungentier.Impfung GROUP BY tm_impfungentier.Tier, tm_impfungentier.Impfung) t WHERE Tier = tm_tiere.id) AS MyAlias
The problem is that due to the implementation of Field::dbField this alias is only used internally and is not passed to mysql. Now one solution would be to find a way to pass that alias actually to mysql. Is there a way to do that?
Edit: i found out the reason why the result column name is truncated it is because of the Alias max length of mysql which is 256 chars. So to get this to work with the editor i really need to be able to alias the subquery in mysql
Thanks for your insight into this! Unfortunately adding the aliasing isn't going to be a trivial task, so for the moment a workaround might be to use a VIEW instead?
Allan
Hi @allan,
using a view is a great idea. I will do that. Actually i can live with this solution. Anyway if somewhen in the future the aliasing is available, maybe you can notice me so we can switch.
Thanks a lot for your awesome support there!