Combining Mjoin and leftJoin
Combining Mjoin and leftJoin
Restful Web Services
Posts: 202Questions: 49Answers: 2
in Editor
Is it possible to combine Mjoin and leftJoin? I get the property_id below but I want to display the property_name.
->join(
Mjoin::inst( 'cms_module_system_tenancies' )
->set( false )
->link( 'cms_module_system_users_tenants.Id', 'cms_module_system_tenancies.tenant_id' )
->fields(
Field::inst( 'property_id' )
->set( false ),
Field::inst( 'tenancy_link_id' )
->set( false )
)
)
->leftJoin( 'cms_module_system_properties', 'cms_module_system_properties.Id', '=', 'property_id' )
->process( $_POST )
->json();
I receive an error message unknown column "property_id" in on clause.
Thanks
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
What table is the
property_name
number?You can use left join and mjoin at the same time, but they aren't really aware of each other.
Allan
Hi Allan,
The
property_name
is coming from the tablecms_module_system_properties
.I store the
property_id
in thecms_module_system_users_tenants
table, but for the user I want to show the human readable name which is only stored in the properties table.Chris
A user can be in more than one property at once which is why I need to use Mjoin.
I don't think you need to use a left join for this - just use an Mjoin with two links (i.e. tell it you have a link table.
Important point: Be careful with link tables if you intend to edit them. Editor will delete delete the rows in the link table and then add them again - so you can't really have any information other than the link between the two tables.
Allan
Hi Allan,
Thanks for your help. I am not sure my structure is correct for this query. I will try and explain.
I have 3 tables:
cms_module_system_tenancies
(the effective Link Table)cms_module_system_users_tenants
andcms_module_system_users_properties
The table
cms_module_system_tenancies
contains the columnstenant_id
andproperty_id
.I use this link
->link( 'cms_module_system_users_tenants.Id', 'cms_module_system_tenancies.tenant_id' )
to get information from the tenants table.This works fine.
However, if I add
->link( 'cms_module_system_properties.Id', 'cms_module_system_tenancies.property_id' )
it does not work.I get the error:
"Unknown column 'cms_module_system_properties.tenant_id' in 'on clause'.
I haven't even specified that column so I am abit lost. Does my objective seem possible?
It sounds like you want to use:
This is assuming that you are reading
cms_module_system_users_properties
as the master Editor table (i.e. showing a list of properties)?Allan
Hi Allan,
Thanks again. I still can not get it to work correctly. Here is my full server side script. I am in the table
cms_module_system_users_tenants
. I want to link to thecms_module_system_properties
table via the link tablecms_module_system_tenancies
. I want to get the columnsproperty_name
(which only exists in the properties table) andproperty_manager
(which only exists in the properties table).Thanks again for your time.
As far as I can see, that should work. Do you get any errors reported? What actually happens when you run that code? Can you show me the JSON that it returns?
Thanks,
Allan
Great, it does indeed work now. A silly error on my part whereby I forgot to enclose
Id
in colons. It should be,Field::inst( 'Id' )
->set( false ),
Thanks for your help.
Just one last issue! I normally display a - character in my table if the value is empty. I sometimes do this client side like so,
But, it doesn't seem to work in this situation. Is there an easy way to display a default value when no value present in the case of using the Mjoin function?
Thanks
Allan
Hi Allan,
Thanks for the reply, that worked perfectly.
However, sorry, I have another last issue! I have the Mjoin function working nicely across various tables. In some cases though where there are a number of records the order of the data is somewhat random.
Is it possible to order the data being outputted in anyway?
For example if in the below example I wanted to order by
position
how would I go about doing that?https://www.datatables.net/forums/discussion/21444/join-tables-one-to-many-join-example-ordering-access-options-alphabetically
I have found the following forum post which suggests it is possible but I don't fully understand the code example and have failed to implement it into my code successfully.
Am I on the right lines, will this example work for me?
Thanks
Chris
Hi Chris,
The link you provided is specifically about the options list in the Editor form. Is that what you are interested in setting, or is it the order for how they are displayed in the DataTable?
If the latter (which I suspect is the case from what you have written above) then you would need to do the ordering in the
columns.render
method. There is currently no option in the PHP to order that data.Allan
Hi Allan,
Thanks again for your response, I have to say the level of support you provide is exceptionally good.
You are correct, it is the order the data is displayed that I am concerned with. I previously manipulated my data using the
columns.render
option like so,This outputs the data in the 'prices' column like so,
However, as you can probably see it would be more logical if it were in order.
I have updated the
render
function to use this sort functiondata.sort(function(a, b){return a.quantity > b.quantity? 1: -1;});
and it seems to work, although I don't know if this is the most appropriate solution?This outputs the data in the 'prices' column like so,
Should that work in all cases?
Thanks
Chris
Hi Chris,
Thanks :-). And yes, your solution looks spot on to me. As long as
data
contains objects which have valid numbers for thequantity
property, then yes, that will work nicely.Allan