JOIN an MJOIN
JOIN an MJOIN

I asked this long ago, and the answer was no way, but I thought I would check again and see if there has been any update.
I have a lot of tables that link 2 other tables. For example, I have "Clients", I have "Hotels" and I have a lookup table called "ClientHotels". I do this because each hotel can have multiple clients, and each client can have multiple hotels.
It every Hotel belonged to one Client, I could MJOIN a list of Hotels inside the details pane of the Clients table. Is there any way to accomplish the same thing... list of Hotels under the Clients table, and list of Clients under Hotels table... using the lookup table "ClientHotels" to link their IDs?
I suppose I could make a VIEW that joins one, and another view that joins the other, then each table could MJOIN the view... is that the only way to do it?
This question has an accepted answers - jump to answer
Answers
"Is there any way to accomplish the same thing... list of Hotels under the Clients table, and list of Clients under Hotels table... using the lookup table "ClientHotels" to link their IDs?"
Sure that is what an Mjoin is made for! But maybe I don't understand the question?!
Here is an example:
This table "ctr_installation" has multiple public authorities, principals and departments. And these are n:m relationships, not 1:n relationships in the relational data model.
Here is an excerpt of the data model (MySqlWorkbench):

I marked the Mjoin Link Tables in yellow. Looks kind of greenish now ...
This is what it looks like in the data table. The Mjoin cols are marked in yellow. The advantage of the MJoin is that you can edit the link tables with Editor.
And you can also have it vice versa: Showing the installations a department belongs to as opposed to showing the departments of an installation:

This is the php code with multiple MJoins:
And this is the php code with the installations a department can belong to (opposite case using the same link table "ctr_govdept_has_ctr_installation":
if you don't need to edit the relationship in your respective use case you do not have to use the mJoin because it is rather limited. You can't return arrays using really complex queries with it. What you can do is to imitate an Mjoin using your own SQL. Like this for example:
The function "getFormatterGovArray" returns the same format as an Mjoin would do and you can use the result at the front end like it was a real Mjoin. As you can see you would be unable to achieve the same result with a real Mjoin due to its limiations.
Hope that helps! Good luck!
Wow, that is awesome. Thanks. Amazing work.
If you don't mind posting a little more... how do you get the Selection of Principals (selectize?) box to send data to Editor? ...what does its php-api file look like (you can't use the one with joins for yout editor right)? ...and I would like to see your code that populates its options too? I do have a way to do both, but I'm guessing your way might teach me a thing or two
Thanks again.
"how do you get the Selection of Principals (selectize?) box to send data to Editor?"
These are the Editor field definitions on the client side for the two Mjoins "Public Authority Selection" and "Selection of Principals". That's all you need. This communicates directly with the two PHP-Mjoins above.
"and I would like to see your code that populates its options too?"
You've seen it already: It is the two options instances INSIDE the Mjoins.The Mjoins on "gov" and "user" both have options instances that populate the selectize fields on the front end.
The third Mjoin on "ctr_govdept" doesn't have an options instance because it is only used to display the departments not to select them with Editor.
So two of the three "Mjoin-arrays" from PHP (or whatever you want to call them) are used in Editor - and hence have options instances that are used by Selectize (or select or select2 or whatever you want to use). One of the three is not used in Editor - and has no options instance.
All three of them are displayed in the data table like this. Yes, you can do the rendering more elegantly but it works:
If you only need to display data and can't or don't want to use an Mjoin you can use a function like "getFormatterGovArray" that returns the same format as an Mjoin would do. The rendering for this on the front end side can be very simple like this:
The trick in PHP is to alias an existing MySQL-table field and use this "container" to get the pseudo-Mjoin-Array from PHP to the front end:
I am using "ctr_govdept.id" and alias it to "gov". This also makes it very easy to pass this id into the gefFormatter that creates the "Mjoin-array". It is the parameter $val that Editor reads from the database.
That's a heck of a solution! Thanks for posting all those details @rf1234.
The other option is to use a VIEW as @rpmccormick originally noted. There is no
leftJoin
option inside theMjoin
instance at this time.Allan
@allan, yes views are really useful for this! Here is a more complex example from the world of "financial weapons of mass destruction" (Warren Buffet). Let me call them "derivatives" which is shorter ...
Many German municipalities still have this stuff on their books. Their use is forbidden these days when it comes to mere speculation. According to the law you must assign an "underlying" contract that is "hedged" by the derivative.
In terms of my data model both, the derivative, e.g. an interest rate swap and the underlying, e.g. a variable rate loan financing a school for example, are contracts. (The benefit of the swap could be that it will eliminate the interest rate change risk when done properly. But of course you can ask the question: Why would you finance a school with a variable rate loan in the first place?)
So what the Mjoin needs to do is to assign one ore more contracts (the underlying(s)) to the derivative. Even more complicated: One underlying can be hedged by multiple derivatives and one derivative can hedge multiple underlyings (n:m relationship).
This only works with a view that selects the potential underlyings from the contract table. Here is an excerpt from my data model that shows this. The foreign keys in the link table "derivative_has_underlying" are the contract id of the derivative and the contract id of the underlying. Hence the link table links the contract table with itself.

And this is the Mjoin that does the job:
This is the front end code for it:
and yes: you can only assign an underlying to a derivative not to "normal" contracts. This is being resolved with "dependent":