Joins with multiple related tables
Joins with multiple related tables
Can I have two separate tables joined to my main table? I have a trouble ticket app where a ticket may have zero or more notes related to it, and one vehicle related to it. I've built 3 apps that have the 1-to-many notes relationship, and they work just fine. When I tried to add the 1-to-1 vehicle relationship in the new app, I found that the editor panel open up empty (no contents or labels) for either a create or an edit, for either the tickets or the notes tables. (The relationship with the vehicles table is read-only - just to allow a vehicle to be selected.) I have verified that the vehicle data gets to the DataTable, as it is displayed correctly there. The problem is just with the tickets editor and the notes editor.
If it's necessary to look at code, I'll make arrangements for you to see it privately.
Thanks,
Tom
This question has accepted answers - jump to:
Answers
"The relationship with the vehicles table is read-only - just to allow a vehicle to be selected"
What you need is to be able to assign one vehicle to a ticket. That is done with an options instance and you would save the vehicle id as a foreign key in your ticket table. Alternatively you could use a link table if you don't want to have the foreign key in your ticket table. A left join would only be needed to retrieve a vehicle name or something else to display in the select field at the front end.
https://editor.datatables.net/manual/php/joins#Options
Here is an example with two options instances:
https://editor.datatables.net/examples/advanced/joinLinkTable.html
One options instance is used to populate the foreign key field "site" in the parent table "users". The other instance is used to populate the link table "user_dept" between "users" and "dept".
I attached a graph of the data model to make it easier to understand the example.
I've built several apps that use selections from a related table, and they work just fine. I've also built apps that edit in a related table, along the lines of this blog post: https://datatables.net/blog/2016-03-25, and those have worked as expected.
I misspoke slightly in my question, in that the ticket needs not only to allow a vehicle to be selected, but also to render data from the vehicles table that allows users to identify the vehicle. Those requirements are in addition to supporting editing in the related notes table (wherein a ticket may have zero or more related notes).
My question about tables joined to a given table arises from my frustration at being unable to see how I've done this incorrectly. My descriptive field identifying the vehicle from the joined table displays correctly, so I believe that has been done correctly. The empty editor panel that results is the thing that is troubling me that I haven't been able to solve. I had a similar type of problem a couple of months ago trying to implement my client's request to have two editors open at once, when I inquired and found out that this is not currently supported. I asked this question so I would not repeat that experience again, in case this is not possible.
Now I'll have to find a way to extract a simple example from this.
Thanks for your patience
"I misspoke slightly in my question, in that the ticket needs not only to allow a vehicle to be selected, but also to render data from the vehicles table that allows users to identify the vehicle. Those requirements are in addition to supporting editing in the related notes table (wherein a ticket may have zero or more related notes)."
There might be an issue with your JavaScript?! It is no problem to join multiple tables to provide data to render the options instance in Javascript. Here is an example from my own coding. This options instance renders the data from two tables and it INNER JOINs them (via the WHERE clause because Editor doesn't allow explicit INNER JOINs). In addtion I LEFT JOIN these tables outside the options instance in order to have those fields available for rendering inside the options instance. Sounds weird but it works fine:
PHP:
JavaScript: The department name is rendered at the front end as a combination of two fields of two different tables: $row['gov.name'].' / '.$row['govdept.name'];
BUT: the field name in JavaScript is the foreign key that you want to populate by selecting a department.
I tried your approach, rendering the vehicle identification in PHP, as this really simplifies the Javascript, where I've been unable to resolve this problem. (My original code had rendered this correctly in Javascript, but it made ugly looking code.) I'd thought I had this worked out, but now I get a "Warning - Invalid JSON" diagnostic. The JSON looked OK to me, so I ran it through JSONlint, and it was pronounced as valid. I'm not sure what to do with this.
I also noticed that I'm getting no data for my fields from the vehicles table, and I don't see what that is happening. I ran the debugger for this, and the code for the debug file is UKUYEF. My code for the tickets table editor is here:
```
Editor::inst( $db, 'tickets', 'id' )
// ->debug(true)
->fields(
Field::inst( 'tickets.id' )->set(false),
Field::inst( 'tickets.created' )
->set(Field::SET_CREATE)
->setValue(date("Y-m-d H:i:s"))
->getFormatter( function( $val, $data, $opts) {
return date("Y-m-d H:i:s", strtotime( $val) );
} ),
Field::inst( 'tickets.updated' )
->setValue(date("Y-m-d H:i:s"))
->getFormatter( function( $val, $data, $opts) {
return date("Y-m-d H:i:s", strtotime( $val) );
} )
->setFormatter( function( $val, $data, $opts) {
return date("Y-m-d H:i:s", strtotime($val));
} ),
Field::inst( 'vehicles.bareTag' ),
Field::inst( 'vehicles.color' ),
Field::inst( 'vehicles.modelYear' ),
Field::inst( 'vehicles.maker' ),
Field::inst( 'vehicles.model' ),
Field::inst( 'vehicles.type' ),
Field::inst( 'tickets.vehicle_id' )
->options( Options::inst()
->table( 'tickets,vehicles' )
->value( 'vehicles.id' )
->label( array('bareTag','color','modelYear','maker','model','type' ) )
->render( function($row) {
return $row['vehicles.bareTag'] . ' - ' .
$row['vehicles.color'] . ' ' .
$row['vehicles.modelYear'] . ' ' .
$row['vehicles.maker'] . ' ' .
$row['vehicles.model'] . ' ' .
$row['vehicles.type'];
} )
->order('vehicles.bareTag asc')
->where( function($q) {
$q ->where( 'vehicles.id','tickets.vehicle_id','=',false );
} )
),
Field::inst( 'tickets.status' )
->validator( Validate::notEmpty() ),
Field::inst( 'tickets.originator' )
->validator( Validate::notEmpty() ),
Field::inst( 'tickets.responder' )
->options(Options::inst()
->table('responders')
->value('responder')
->label('responder')
->where( function($q) {
$q->where('active','yes','=');
} )
->render( function ($row) {
return $row['responder'];
} )
),
Field::inst( 'tickets.category' )
->options(Options::inst()
->table('categories')
->value('category')
->label('category')
->render( function ($row) {
return $row['category'];
} )
)
// Field::inst( 'vehicles.id' )
// ->options( Options::inst()
// ->table('vehicles')
// ->value('id')
// ->label('bareTag' . ' ' . 'color' . ' ' . 'modelYear' . ' ' . 'maker' . ' ' . 'model' . ' ' 'type'),
// ->label('bareTag')
// )
// ),
)
->where( function($q) {
global $vehicleId;
error_log('t.a.p2: vehicleId= ' . $vehicleId);
if ($vehicleId != '') {
$q ->where('tickets.vehicle_id',$vehicleId,'=') ; // Fetch this vehicle's tickets
}
} )
->on('postCreate', function($editor,$id,$values,$row) { // Notify techs of new ticket
error_log('f.t.p: Created: id= ' . $id . ', catR= ' . $row['category'] . ', descR= ' . $row['description'] . ', catV= ' . $values['category'] . ', descV= ' . $values['description']);
notify($id,$values['category'],$values['description'],$values['email']);
} )
->where( function ($q) {
global $vehRole,$maxAge,$theUser;
if ($vehRole != 'tech' and $vehRole != 'admin') {
$q ->where('tickets.originator',$theUser,'='); // Restrict non-(tech,admin) to own tickets
}
if ($maxAge != 'all') { // Exclude old closed tickets
$q ->where('tickets.status','closed','!=')
->or_where(function( $r) use ($maxAge) {
$r->where('tickets.status','closed','=');
$r->where('tickets.updated','date_sub(now(),interval ' . $maxAge . ')','>',false);
} );
}
} )
->leftJoin( 'vehicles','vehicles.id','=','tickets.vehicle_id' )
->process( $_POST )
->json();
``
I think I can straighten out my Javascript once I get this PHP to work correctly.
Thanks,
Tom
Hard to read this without markdown, Tom.
There are a couple of problems here I guess:
You don't need the INNER JOIN because you are just retrieving the label from one single table. The same table that you get the foreign key from: "vehicles". In my case I got the label from two tables different from my primary table.
Secondly your label array field names aren't corresponding with what you select in your LEFTJOIN.
This could work - I didn't check your other options instances though:
I am pretty sure you don't need the tablename as a prefix on the fields selected with the options instance if you only have one table. This should work:
If not prefix it whith "vehicles".
So this is my suggestion for this options instance:
Hi Tom,
The reply from the server for table
#tickets
has an error at the start of it which is making it invalid JSON:Allan
Hi rf1234,
Your answer about the join and the render in PHP helped me understand how to do this correctly. (It turns out that I needed to use ->value( 'vehicles.id' ) to make it work.) I think that cleared up what I was misunderstanding about those things, and I thank you for that. However, I still have the original unresolved issue:
My editor panel is still empty when I click New or Edit, which was the original reason I asked a question here. I was hoping that getting the rendering of the options fixed might make this go away, but it did not. So, I still need to find a way to deal with that one.
Thanks again for your help,
Tom
If the json error is fixed now the next step would be to look at your JavaScript. If you can post that I can take a look.
rf1234,
Thanks for the offer, but Allan responded to a tech support purchase I made, and fixed that issue. It turns out that the names were inconsistent in the editor fields, which I had not noticed on my own. That problem is now behind me, and I'm dealing with another issue. I appreciate your help, as that part of the program is now working correctly.
Thanks again,
Tom