Editor: link and edit two tables
Editor: link and edit two tables
Hello,
my problem concerns the editor:
i am at a loss as to how i can link two tables one to one and edit the individual fields of both tables.
I would like to be able to edit the "NoteInternal" and "Hours" fields in the editor.
The desired result should be:
A data set from "ITSM_Maintenance_TaskAsset" is linked to only one data set from "TimeAccounting
2 Tables:
ITSM_Maintenance_TaskAsset
----
id
NoteInternal
timeaccounting_id (linked with TimeAccounting.id)
TimeAccounting
---
id
Hours
<?php
require_once '../../../../configs/main.php';
require_once '../../../../libs/datatables-editor/php/DataTables.php';
use DataTables\Editor;
use DataTables\Editor\Field;
use DataTables\Editor\Join;
$data = Editor::inst($dbIntranet, 'ITSM_Maintenance_TaskAsset')
->debug(true)
->fields(
Field::inst('ITSM_Maintenance_TaskAsset.id')
->set(false),
Field::inst('ITSM_Maintenance_TaskAsset.NoteInternal'),
Field::inst('ITSM_Maintenance_TaskAsset.timeaccounting_id')
)
//only this did not work, the left joined table will not update
//->leftJoin('TimeAccounting', 'TimeAccounting.id', '=', 'ITSM_Maintenance_TaskAsset.timeaccounting_id')
//this throws an error
//Join was performed on the field 'timeaccounting_id' which was not included in the Editor field list. The join field must be included as a regular field in the Editor instance.
->join(
Join::inst('TimeAccounting', 'object')
->join('timeaccounting_id', 'id')
->field(
Field::inst('id')->set(false),
Field::inst('Hours')
))
->process($_POST)
->data();
echo json_encode($data);
This discussion has been closed.
Replies
Use the
leftJoin
method, but make sure that you include theTimeAccounting.id
field in the client-side fields that will be submitted (usehidden
so the end user doesn't see it). Editor needs the primary key value to be able to edit the second table.Allan
Hello Allan,
thanks for your quick reply.
I have created a test table, but it does not yet work as intended.
furthermore, only entries in "TEST_Table1" are created/updated
You need to add
Field::inst('TEST_Table2.id')
in your list of fields in the PHP. Otherwise the value isn't included in the data read from the database, which is why it is then being submitted as empty.Allan
unfortunately it still doesn't work:
Ah - this is on create. Does it work on edit before we attempt the create?
Allan
Hello Allan,
only if I add the entries in both tables manually and link them together beforehand, the subsequent editing in "datatables" is functional:
but if i want to create a new entry:
I'm with you now - thanks! The problem is that Editor's edit action will attempt to insert into the main table first and the joined tables second. That means the entry for the joined table hasn't yet been created and thus there is no primary key value to write into the main table for that link.
I'm afraid this is a limitation of the PHP libraries at this time. It is something we plan to address for 1.8 though.
At the moment you'd need to edit the other tables and then edit the master table.
Allan
Okay, when will it come out
I don't have a firm release date yet I'm afraid. I expect it to be August though.
Allan
Hi,
Good day.
Having similar issues. Is this a PHP limitation only? Or does node suffer from the same problem?
Regards.
Running into this issue with Editor 1.9.0.
From Alan above:
I'm using leftJoin to an Address table from a User table. I'm trying to provide UI where the user can add (and/or update) their address. The record in the Address table is being created, but I'm not getting the address primary key record linked into the main user table record.
I do have the record id's (from both tables) as part of the Editor fields.
Looking for guidance.
I'm afraid that's not something the Editor libraries provides support for at the moment. It never made it into 1.8 or 1.9 (its something that would have held up those releases).
As I mentioned before, the workaround is to have your user edit two different tables I'm afraid. Our you could use server-side event handlers to check if a new contact is needed or needs to be updated.
Allan