Join with table rename
Join with table rename
marianidiego
Posts: 54Questions: 17Answers: 1
in Editor
Link to test case: https://editor.datatables.net/examples/advanced/joinArray
Description:
It would be interesting to be able to change the name of the linked table. Like: "Mjoin::inst( 'permission as permission' )" .
This might be useful in some circumstances....
Replies
... and it is already possible!
Just create a view in you database and use it.
Example from my own coding:
- I created a view on "contract" which I call "underlying". Then I created a link table that links derivative contracts with their "underlying" contract. Meaning a link table between "contract" and itself.
Now I can create an Mjoin that allows me to select underlyings for derivative contracts. Both are in the contract table of course. ("Naked" derivatives are illegal for my clients. They must have and assign an "underlying" contract they want to hedge.)
This is the Mjoin:
Not exactly.
I already use views in sql...
But exactly, these each one has a different name. I have different views, to extract data for different circumstances. If I can rename the table to datatables, I can use the same code each time.
If I declare a
Editor::inst( $db, 'product_special_clients as products', 'products_id' )
this will work without any problems.
But if I use a
This will not work.
ok, then define those different table names as (global) variables and get them into the code that way. Shouldn't be any problem.
If you have trouble having the right table name available in your code use a $_SESSION variable for example that you can set elsewhere depending on your context. That way your code becomes very flexible.
Here is a WHERE clause that is different for each context depending on how certain session variable are set. For certain contexts the WHERE clause isn't applicable at all for example, see first "if" condition. Hence I can use the same Editor instance for client users with restricted rights and for internal admin users with unlimited rights. I also use different table names in the SQL depending on the situation.
I need to look at what will be involved in supporting that directly. I can see that it might be useful. Thanks for the suggestion.
Regards,
Allan
@marianidiego - I've just been looking at this, and I'm wondering if you could tell me a little more about what sort of use cases you had in mind here?
There are a few goals that I can think of:
permissions
refers to itself for example. That is actually possible already using theMjoin->aliasParentTable()
option which can be used to give the parent table an alias.Mjoin->name()
.Is there anything I've missed?
Allan