MJoin Multiple Databases
MJoin Multiple Databases
I have succesfully been using multiple databases in my application by simply adding the database name before the table name like so:
Editor::inst( $db, 'ridedata1-web1-uk.ride', 'ride_id' )
However, when attempting to use the same method with MJoin it is not working and I recieve the following SQL error:
An SQL error occurred: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax
Here is my code:
->join(
Mjoin::inst( 'ridedata1-web1-uk.ride_carbon_data' )
->set( false )
->link( 'ride.ride_id', 'ride_carbon_data.ride_id' )
->fields(
Field::inst( 'carbon_benefit' )
->set( false )
->getFormatter(function ($val, $data, $field) {
$val = ($val / 100);
return number_format($val, 2, '.', '').'<br> kg / miles';
}),
Field::inst( 'distance_replace' )
->set( false )
->getFormatter(function ($val, $data, $field) {
$val = ($val / 100);
return number_format($val, 2, '.', '').'<br> miles';
})
)
)
I have used many Mjoins in other places but always when trying to link a table in the same database. I have tried enclosing the database name in ` and " and ' but nothing helps.
Can anyone help me out?
Thanks
Chris
Answers
Hi Chris,
This is an area that isn't well tested in the Editor libraries I'm afraid.
Could you add
->debug( true )
before theprocess()
call in the Editor chain and then show me the JSON that is being returned by the server when the page is loaded? That should contain the SQL query and let me see why it is failing.Allan
Had a similar issue even though it was in the same database. It was about linking two rows from my contract table. One being a "derivative" the other one being the "underlying". Aliasing the table name didn't work. Same problem that you have with this line
Obviously Editor doesn't really escape complex names properly in this statement and I needed a simple name that doesn't contain a period or an alias.
So I created a view called "underlying" and used that in the Mjoin:
This post shows that such a view can even be created across databases. You could call it "carbon_data_view". That should work.
https://stackoverflow.com/questions/10694601/creating-view-across-different-databases
Good luck!
Hi Both,
Thanks for your responses. Here is the debug JSON requested by Allan.
Let me know if you need anything else?
Thanks
Chris
Hi Allan,
It is clear that the error is on the second query, the SELECT DISTINCT, query. The name is not correctly being encapsulated by the ` escape. However, having had a brief look through the PHP for Editor I cant see the exact place where this encapsulation is being done. Can you help point me in the right direction?
Thanks
Chris
Thanks Chris,
This is the statement that is failing:
The
FROM
clause isn't being correctly escaped.Could you show me your full PHP script for the Editor initialisation please?
Thanks,
Allan
Thanks for your reply. Can I email it to you?
Hi Allan. I have sent it as PM message, let me know if you haven't received it.
Hi Allan. Sorry, the first PM I sent you was the wrong one. I have just sent the correct code.
Thanks - I've got it now and will review shortly.
Allan
That looks pretty much like the same thing causing my issue.
I had something like
Sinc this wasn't escaped properly I devised the work around with the view. Would be good to hear back from you guys to know whether I'll still need the work around in the future. Many thanks.
Yeah -
as
most certainly won't work in MJoin at the moment I'm afraid. That is something that needs to be added into the libraries.I don't believe that is the issue with @Restful Web Services's issue though. It looks like there might be an issue with the detection of the database fields. More soon.
Allan
Thanks for the update.