Mjoin and alias
Mjoin and alias
Hi,
I want to do a relation one to multiple.
I have a table "centers" with this fields : "cen_id" and "cen_title".
And another table "centers_centers" with this fields : "cc_cen_id" and "cc_parent"
The content of cc_parent is the cen_id of the parent center. So one center can have several parents centers.
So I use a Mjoin for my one to multiple relation but I have to use 2 times the "centers" table. This is impossible in mysql and I receive an error. I want to use an alias like with leftJoin but it seems impossible, I receive an error too.
What is the solution ?
Thanks,
Gaétan
This question has an accepted answers - jump to answer
Answers
The
Mjoin
class has a method calledname()
which can be used to modify the JSON name for the resulting data:That basically provides an alias so you can use the same target Mjoin table multiple times. This shouldn't create an SQL conflict as it is actually a separate SQL statement that Editor executes for each Mjoin.
Allan
Hi,
Thanks for your answer, it's seems to be the good issue :)
But, this is my code :
And I receive :
{"error":"SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table\/alias: 'centres'","data":[]}
Ah I see - thanks for the code. You need to use the
Mjoin->aliasParentTable()
method in that case - it will do a proper alias:Then refer to
c2
as the table name for the parent table.Allan
Hi Allan,
Sorry, I don't understand why my code doesn't work...
This is my code with name for the alias of my Join table and aliasParentTable for my master table.
I receive : {"error":"SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table\/alias: 'c2'","data":[]}
Where is wrong ??
Thanks again ;)
Could you open the file in
Database/Drivers/Mysql/Query.php
and in the_prepare
function you will see a commented out debug line (file_put_contents()
) could you comment it back in and change the location that the file writes to (if you need to) then run the script again please.It will dump the generated query to that file. Then copy and paste the queries shown into this discussion so I can take a look.
Allan
With this code, the sql is :
There isn't "centrealias" ? And two times "c2"
Edited by Allan - formatting of the SQL for readability
Hi,
Sorry for the delay in replying! Two issues here:
centrealias
is a concept that is entirely in PHP / JSON - it is not used in the SQL at all so it shouldn't be referred to in the SQL field names.Join.php
file replace the_prep
method with the following which has the fix in place:Regards,
Allan
Hi Allan,
This doesn't work.
With this code :
I receive :
{"sError":"Table selected fields (i.e. '{table}.{column}') in
Join
must be read only. Useset(false)
for the field to disable writing. --- name -> centre_copy.cen_id --- set -> both --- dbField -> centre_copy.cen_id --- strpos . dbField -> 11 --- "}And with this code :
The query is :
SELECT centres.cen_id as dteditor_pkey, centres.cen_id as cen_id, centres.cen_titre as cen_titre FROM centres as c2 JOIN
centres_mut
ONc2
.cen_id
=centres_mut
.cm_cen_id
JOINcentres
ONcentres
.cen_id
=centres_mut
.cm_mut
So, this query don't return the list of attached centres.
I have an error or it's a bug ?
Thanks again,
Gaétan
edited by Allan Code highlighting
Hi Gaétan,
This line is wrong. As I say, the
name()
should not be referred to in the SQL field names. I would suggest trying:p.s. Details on how to highlight code using markdown can be found in this guide.
Yes but this code return :
{"data":[{"DT_RowId":"row_1","centres":{"cen_id":"1","cen_titre":"centre 1","cen_exclusivite":"1","cen_date":"0000-00-00 00:00:00"},"centre_copy":[{"cen_id":"1","cen_titre":"centre 1"}]},{"DT_RowId":"row_2","centres":{"cen_id":"2","cen_titre":"Centre 2","cen_exclusivite":"0","cen_date":"0000-00-00 00:00:00"},"centre_copy":[{"cen_id":"2","cen_titre":"Centre 2"},{"cen_id":"2","cen_titre":"Centre 2"}]},{"DT_RowId":"row_3","centres":{"cen_id":"3","cen_titre":"centre 3","cen_exclusivite":"1","cen_date":"0000-00-00 00:00:00"},"centre_copy":[{"cen_id":"3","cen_titre":"centre 3"},{"cen_id":"3","cen_titre":"centre 3"}]},{"DT_RowId":"row_4","centres":{"cen_id":"4","cen_titre":"centre4","cen_exclusivite":"0","cen_date":"0000-00-00 00:00:00"},"centre_copy":[{"cen_id":"4","cen_titre":"centre4"}]}],"options":{"centres[].cen_id":[{"value":"1","label":"centre 1"},{"value":"2","label":"Centre 2"},{"value":"3","label":"centre 3"},{"value":"4","label":"centre4"}]},"files":[]}
For each "centre", I receive a list of attached centers. The number of attached centers is ok but data are data of the master center :
"centre_copy":[{"cen_id":"2","cen_titre":"Centre 2"},{"cen_id":"2","cen_titre":"Centre 2"}]
And if I use :
I receive :
{"sError":"Table selected fields (i.e. '{table}.{column}') in
Join
must be read only. Useset(false)
for the field to disable writing. --- name -> centres.cen_id --- set -> both --- dbField -> centres.cen_id --- strpos . dbField -> 7 --- "}Try this:
That will cause the join to read the fields from the joined table. You don't really want or need the table selector there.
Allan
Yes, I've tried this.
The query is :
SELECT
cen_id
as 'cen_id',centres
.cen_id
as 'centres.cen_id',centres
.cen_titre
as 'centres.cen_titre',centres
.cen_exclusivite
as 'centres.cen_exclusivite',centres
.cen_date
as 'centres.cen_date' FROMcentres
SELECT centres.cen_id as dteditor_pkey, centres.cen_id as cen_id, centres.cen_titre as cen_titre FROM centres as c2 JOIN
centres_mut
ONc2
.cen_id
=centres_mut
.cm_cen_id
JOINcentres
ONcentres
.cen_id
=centres_mut
.cm_mut
SELECT DISTINCT cen_id as value, cen_titre as label FROM
centres
ORDER BYlabel
ascSo, I receive an array of attached centers for each center like this :
"centre_copy":[{"cen_id":"2","cen_titre":"Centre 2"},{"cen_id":"2","cen_titre":"Centre 2"}]
The number of attached centers is ok but the data are data of the master center.
Are you able to e-mail me a dump of your
centres
andcentres_mut
tables please (allan@ this domain.net
)? I'll try to recreate and debug the issue locally.Allan
Thanks for your answer by email Allan :)
I always have a problem about this but with edition.
The request return this :
Look at :
"options":{"centres[].cen_id":[{"label":"Wiig","value":"2"},{"label":"centre 1","value":"1"},{"label":"centre 3","value":"3"},{"label":"centre4","value":"4"}]},"files":[]}
It's "centres[].cen_id", I think that is a conflict with other data with alias “centres". So, when I edit a line, the request for save data contains centres array without title, ...
I think that the identifiant option must be centre_copy[].cen_id
This is my code for Editor in js :
You are correct - thanks for pointing that out.
The workaround for the moment is to use
initComplete
:Allan
Thanks, It's works !