Self join
Self join
I have a single table which contains records which are date specific, i.e. past, present and future based on today's date.
I use a where filter to ensure I only show the right records in the right table; i.e. past table, present table, etc.....
Should I, in theory, be able to do a self join in order to show a value in a column when a particular client has multiple records, i.e. if the client has a future and current record then the column would show, for example, the value 'Existing Client'?
I have attempted to add a left join like so,
->leftJoin( 'cms_module_system_tenancies as tenancy', 'cms_module_system_tenancies.Id', '=', 'tenancy.Id' )
This works but it seems that my original WHERE filters are still being used on the leftJoin, which makes sense, however it means that I can still only see 1 record instead of 2 in the column.
Do I need to use ONE-TO-MANY join and if so is that possible when self referencing a table?
Thanks
Chris
Replies
You should be able to use leftJoin with an alias. Could you show me your full PHP code please?
Allan
Hi Allan,
It looks like this.
The field tenancy.tenancy_link_id should be a single value or multiple values if there is a current and future tenancy for that person.
Cheers
Chris
Hi Chris,
With a left join it can only be a single value (or null). If you are expecting an array of values, you would have to use a one-to-many join (
Mjoin
).It sounds like you need to modify the code to use
Mjoin
based on your description. For that you will probably want to use thealiasParentTable
method in order to provide the alias.Allan
Can I use a different WHERE in a left join? I don't need the specific value I just need to know if it exists or not. So when I am in the 'Future' table I just need to check the 'Current' data to see if the tenant_id is also present.
If Mjoin is the only option should I experience any problems when trying to reference the same table or should it be fine?
No - this isn't how the left join is working if you could have multiple results. The result of a left join is a flat table. If you want to check if there are one or more joins to a record you will need to use
Mjoin
.As I mentioned you should use
aliasParentTable
, but yes it is possible to self join.Allan
Hi Allan,
Thanks for your help. I have a few Mjoin functions working in other tables but I cannot get this one to work when using
aliasParentTable
.Could you please take a look at my code and see if you can tell my why I am recieving the datatables.net//tn/4 error "table id=cms_module_system_tenancies - Requested unknown parameter 'cms_module_system_tenancies.Id' for row 0."
Thanks
Chris
Hi Chris,
Could you show me the JSON that is being returned by the server? I suspect, since you are using an Mjoin, you will need to use array syntax in the property. For example
cms_module_system_tenancies_copy[].Id
.If you have a link to the page that would be very useful.
Allan
Hi Allan,
Yeah sure, it looks like this.
There should be two tenancy_link_id values in the cms_module_system_tenancies_copy data.
Cheers
Chris
Try using:
As you will be able to see from the above JSON, it is an array with an object, that has a single field:
The single field corresponds to the single
Field::inst( 'tenancy_link_id' )
in the Mjoin configuration.Allan
Hi Allan,
If I add a value for
->name
then it seems to work but I am getting duplicate values. So for example the below should just return DONTDELETEONE and DONTDELETETWO but I am getting them twice.Can you see anything wrong?
Thanks
Chris
Hi Chris,
Can you show me the Javascript Editor code please? Are you attempting to edit both values in the Mjoin? If so, that I'm afraid won't work if they are spread over two fields.
It is possible to create a custom field type that would submit the two values in a single object if you need to be able to do that - it would also have to cope with the array of data, and this isn't something the built in field types can do.
Regards,
Allan
Hi Allan,
I don't want to edit the values. I just want to display the values. I have used
->set( false )
, is that not sufficient?Chris
Oh I see - sorry. I had assumed that was after an edit.
I'm afraid that isn't an issue that I immediately recognise so I don't have an immediate solution. We'd need to do some debugging - in the
Database/Drivers/Mysql/Query.php
file you'll find a commented out linefile_put_contents
. Can you comment it back in and update the path to be suitable for your server.What will output the SQL Editor uses to the target file. If you run those queries, what is the output? Does it show the duplicates? If you could send me a dump of your database and the full PHP code you are using for Editor I can debug it here.
Allan