Edit both fields of linked mjoin table?
Edit both fields of linked mjoin table?
I have a table structure similar to the example here: https://editor.datatables.net/manual/php/mjoin#Mjoin-class
My first table is "mechanics" with user_id and trade. The second table ("building_areas") uses building_area_id to identify the areas inside our buildings - it includes the building name and associated area name. The link table is "mechanic coverage" which is the mechanic id's and building_area id's. The mechanic id shows up several times in this table if the mechanic covers several areas.
I have successfully set up the mjoin and the table data is displaying as I had hoped. (The mechanic name, trade, and a concatenated list of the buildings and areas he/she covers). Picture attached.
My issue is that the field I would like to be able to edit (in the link table) is the mechanic id. When a mechanic calls in sick or has time off, a different mechanic covers those areas.
Editor::inst( $db, 'mechanic', 'user_id' )
->field(
Field::inst( 'mechanic.days' ),
Field::inst( 'mechanic.shift_start' ),
Field::inst( 'mechanic.shift_end' ), ...
)
->join(
Mjoin::inst('building_areas')
->link('mechanic.user_id', 'mech_coverage.today_mechanic')
->link('building_areas.area_id', 'mech_coverage.building_area_id')
->fields(
Field::inst( 'area_id' )
->validator( 'Validate::required' )
->options( Options::inst()
->table( 'building_areas' )
->value( 'area_id' )
->label( array('building_name', 'area_name') )
),
Field::inst('building_name'),
Field::inst('area_name')
->setFormatter( 'Format::ifEmpty', null )
)
)
I would like to edit mech_coverage.today_mechanic - not the building areas but I haven't been able to figure out how to do it. Is there something simple I'm missing or should I have approached this a completely different way?
A couple of key features I'm hoping not to lose: mechanic shows up only once in display, building_areas are a concatenated list in one column.
Thank you for any help.
Answers
Hi,
Could you show me the client-side code you are using to initialise both your DataTable and Editor please? Your server-side code looks okay there.
Thanks,
Allan
Hi Allan -
Below is the client side code.
I think the crux of the problem is that I am unsure (in the server side code) how to connect the mechanic and person tables (for a select dropdown) and the mech_coverage table (for the edit). Here is the client side code - I'll post a more complete server side code also.
And the complete server side code:
You don't have a
building_areas
field in your Editor field list. I expected there to be something like the Permissions field in this example.Perhaps something like:
or if you prefer a select input:
Regards,
Allan
Hi Allan - thank you for quick reply.
But I do not want to edit the buildings_areas - I want to edit the other field in the link table (mech_coverage.today_mechanic) as a select input.
Is that possible?
I thought because Editor deletes old references before inserting new ones when updating one to many linked data, that it would be just as easy to update the "one" as it is to update the "many" - but maybe not...
If not, any hints on an alternative approach?
Thank you again
Yes, that is actually want Editor's Mjoin does - it doesn't alter the secondary joined table, but rather the link table. This example demonstrates that, and the documentation discusses it.
Regards,
Allan
Hi Allan, I am sorry, I am not sure if I'm not explaining well or if I just don't get it...
I have read through all the documentation and examples several times, but, *** the examples show how to change the permissions of a user. But I want to change the user who has those permissions. ***
I still want the datatable to show one user with multiple permissions - but the only edit I want to provide is the ability to edit the user.
My own tables are slightly different obviously but the idea is the same.
I'll keep plugging away at it, but might need to re-arrange my data if there's no easy way to do it using mjoin.
Thank you by the way - I do appreciate all the responses.
I fear it might be me misunderstand - sorry. If we take the Mjoin example, and you want to edit the user information but not the permission information for that user, you'd drop the
permissions
information in the Javascript Editor (i.e. don't have that checkbox field). The user information would still be editable.Allan
Thanks Allan -
Because I want to edit the "user_permission" table user_id and not the "user" table user_id, I ended up adding a third table (using your example it would be something like "substitute_staff" that holds only the regular user_id and the substitute_user_id.)
The user_permissions table does not get edited at all, but I can show one user with multiple permissions and I can temporarily change the user by changing the user in the "substitute_staff" table.
I can post some code if it would help someone else but I suspect I am the oddball case.
Thank you for all your help with this - much appreciated.