Mjoin - return multiple columns and concatenate
Mjoin - return multiple columns and concatenate
peterbrowne
Posts: 314Questions: 54Answers: 0
in Editor
I have the following Mjoin and want to add another column to be returned. Then concatenate the two returned columns - unit_code
and unit_name
. Is this possible in the code?
->join(
Mjoin::inst( 'unit' )
->link( 'unit_outcome.unit_outcome_pk', 'unit_unit_outcome_lookup.unit_outcome_fk' )
->link( 'unit.unit_pk', 'unit_unit_outcome_lookup.unit_fk' )
->order( 'unit.unit_name asc' )
->fields(
Field::inst( 'unit_pk' )
->options( Options::inst()
->table( 'unit' )
->value( 'unit_pk' )
->label( 'unit_name' )
),
Field::inst( 'unit_name' )
)
)
so something like:
->join(
Mjoin::inst( 'unit' )
->link( 'unit_outcome.unit_outcome_pk', 'unit_unit_outcome_lookup.unit_outcome_fk' )
->link( 'unit.unit_pk', 'unit_unit_outcome_lookup.unit_fk' )
->order( 'unit.unit_name asc' )
->fields(
Field::inst( 'unit_pk' )
->options( Options::inst()
->table( 'unit' )
->value( 'unit_pk' )
->label( 'unit_name' )
),
Field::inst( 'unit_code' + ' ' + 'unit_name' )
)
)
This question has accepted answers - jump to:
Answers
This returns your new field "unit_code_with_name".
Well thanks for this.
In the datatable section of code, I have:
which renders unit_code_with_name just fine.
However, in the editor section,
I don't get anything showing in the Units select list.
I have:
I'm obviously doing something wrong as the datatables view is fine with the unit code and name being concatenated...
The field to edit in Editor is
unit[].unit_pk
. You don't want to referenceunit_code_with_name
in Editor since that is not the edit point. Indeed, @rf123's code has->set(false)
which I heartly agree with.If you need to be able to edit the MJoined data in the Editor, use nested editing.
Allan
Thanks for the reply Allan.
Using the below shows the units in the editor select now, but just the unit_name, but not prefixed with the unit code:
image of the editor:
The concatenated unit_code and unit_name in the table is fine...
so what do I change to get the unit code and the unit name concatenated in the unit editor field?
You need a renderer for your options instance. Here is one from my own coding. Feel free to adapt to whatever is suitable for you.
In this case the rendered label that is shown to the user is even from multiple database tables. So I need a join. I think the Editor options instance still doesn't have a join method so I did a 1990's style join through the WHERE clause. Works
Take a look at these methods
"table" also works with multiple tables that need to be joined
"label" works with multiple fields from the tables above
"render" works with the "label" fields
"where" that is actually the join (an INNER JOIN to be precise: INNER JOIN govdept ON govdept.gov_id = gov.id - "gov" means government agency that can have multiple "govdept" departments.)
Thanks. But your server side code works for the datatable, so those concatenated columns are showing fine there. Eg:
But not in the
Units
select field in the Editor.The problem then seems to be with rendering the concatenated columns in that client side code e.g. the Unit field:
Fine, but that is irrelevant for the options displayed in Editor. The options are NOT the data table content! Funny ... I got confused with that too some time ago. Psycho logic isn't logic unfortunately. We all get trapped with these things every once in a while, I guess.
No, you must do the rendering in the server side code and that is what my example is about. (see "label" and "render" above in my PHP code).
This is the client side code of my example above. As you can see: No rendering there. It is all done on the server.
ok, my example is not an Mjoin but a simple join but the options instance works the same way.
And here I found a nice Mjoin with really complex rendering: ALL DONE ON THE SERVER
And the Javascript of the Mjoin:
This rendering here comes from the server:
Oh! You want to use the rendered value in the dropdown
select
options? Yup, rf1234 is spot on as usual - use the->render()
option of theOptions
class. That class will get the list of options to display - it does not use them directly from the DataTable.Allan
Mmm, actually I already had a solution in the existing code.
So I just used that and modified it for the unit table:
Works fine and I only need to show the unit_code in the datatable (esp given the width of that column).
Thanks guys for your help - greatly appreciated!