How can i add multiple values to a single column in datatable
How can i add multiple values to a single column in datatable
In editor i am usign multiple true to select different values. How do i modify my server-side script to store valules in form of arrays and get them back by joining the values of operator to users table to fetch all the values.
{
label: "Operator",
name: 'users_enc[].id',
type: 'datatable',
multiple: true
},
Field::inst('R.operator', 'editoperator')
->options(
Options::inst()
->table('users_enc')
->value('id')
->label(array('lastname', 'firstname'))
->render(function ($row) {
return $row['lastname'] . ', ' . $row['firstname'];
})
->where(function ($q) {
$q->where('blocked', 0, '=');
$q->where('opsPDC', 1, '=');
})
->order('lastname')
)
->setFormatter(Format::ifEmpty(null))
->setFormatter(function ($val, $data, $opts) {
return implode(',', $val);
}),
Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
This discussion has been closed.

Answers
You need an Mjoin if you want to select and save multiple values in a link table for example.
If you just want to save multiple values in one field, e.g. separated by a comma, then you don't need an Mjoin.
This here doesn't require an Mjoin:
on the server I save the comma separated values in one db-field, but you can also use a setFormatter and alter them before saving:
Some databases do support arrays in a single column (Postgres for example), but it isn't something that Editor's server-side libraries specifically support.
What is the data type of the
operatorfield at the moment?There are a few options, such as storing a comma separated list of the selected id's in the field, but the correct solution will depend a little on how you want to store it in the DB and what your DB is.
Allan
So if you really want to save an array you would need a set formatter that converts e.g. a comma-separated list of values to an array. That could look like this for example (based on my example above):
SO storing the data isn't a problem. Currently Operator(longtext) stores it in form "12,23,43" like this but what i want is to retrieve the values of it users by joining it with users table. I do not know if there is a way to join comma seprated values and retrieve the name
leftJoin('R.operator','users_enc.id','=');
And also i want them to be editable through datatable
No there isn't with Editor and if there was that wouldn't be good data modeling. You would need a link table assigning the selected users to whatever it is and the respectve Mjoin. Please consult the docs about this.
Here is an example:
https://editor.datatables.net/examples/advanced/joinArray.html
Please focus on this:


and this
"user_permission" is the link table between "user" and "permission" it consists of only two columns: "user_id" and "permission_id".
Such a link table would be required in your use case, too.
Yup - 100%. Use an Mjoin. In a relational SQL database you would not easily be able to do a leftJoin on comma delimited foreign key ids like that. That's not really a limitation of Editor, but rather you need to change your data schema in the database to make better use of the database's abilities.
For example using the comma delimited values you won't be able to use referential integrity. (Actually, it is possible in some databases, again Postgres, but it isn't common in SQL).
Allan
As per the need I have created a liniking table which has column(id(pkey),logID and operator).
Now it thorws me an error saying "DataTables warning: table id=runsheetTable - Join was performed on the field 'operator' which was not included in the Editor field list. The join field must be included as a regular field in the Editor instance." I have included R.operator as rregular field
I think the SQL aliasing is confusing it. Could you try
ops_runSheetas the table name, rather than aliasing it asRplease?Allan
I have tried that too still has the same error.
Any updates on this?
Thanks in advance
Your link table looks wrong. It may only contain two columns
a) id of the first table (foreign key)
b) id of the second table (foreign key)
It may not have its own id.
Here is an example from my own data model:

In the example you see it as well:
The link table is called user_permission and it has precisely two columns which are both foreign keys. Please design your Mjoin exactly like this.
Here i have updated my join and database for only two columns but the error still persits.
You are still using an alias here. Could you remove the alias completely and then show me the full initialisation you are using for the PHP
Editorinstance, as well as the latest error message please?Allan
This is the updated version and the error
This is the error i am getting
"{"fieldErrors":[],"error":"An SQL error occurred: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '.logID as 'dteditor_pkey',
users_enc.idas 'id',users_enc.firstname...' at line 1","data":[],"ipOpts":[],"cancelled":[],"debug":["Editor PHP libraries - version 2.2.2",{"query":"SELECTops_runSheet.logIDas 'ops_runSheet.logID',ops_runSheet.idas 'ops_runSheet.id',U.usernameas 'U.username',T.Trackas 'T.Track',ops_runSheet.Directionas 'ops_runSheet.Direction',ops_runSheet.Positionas 'ops_runSheet.Position',ops_runSheet.leadLrvas 'ops_runSheet.leadLrv',ops_runSheet.midLrvas 'ops_runSheet.midLrv',ops_runSheet.trailLrvas 'ops_runSheet.trailLrv',ops_runSheet.sdepartTimeas 'ops_runSheet.sdepartTime',ops_runSheet.arrivalHandoffas 'ops_runSheet.arrivalHandoff',O.Modeas 'O.Mode',ops_runSheet.arrivalHandoffas 'ops_runSheet.arrivalHandoff',ops_runSheet.adepartTimeas 'ops_runSheet.adepartTime',O2.Modeas 'O2.Mode',ops_runSheet.adepartTimeas 'ops_runSheet.adepartTime',ops_runSheet.CoupledTrainas 'ops_runSheet.CoupledTrain',ops_runSheet.commentsas 'ops_runSheet.comments',ops_runSheet.changeOffReasonas 'ops_runSheet.changeOffReason',ops_runSheet.leadMileageas 'ops_runSheet.leadMileage',ops_runSheet.midMileageas 'ops_runSheet.midMileage',ops_runSheet.trailMileageas 'ops_runSheet.trailMileage',ops_runSheet.leadendMileageas 'ops_runSheet.leadendMileage',ops_runSheet.midendMileageas 'ops_runSheet.midendMileage',ops_runSheet.trailendMileageas 'ops_runSheet.trailendMileage',A3.lrv_noas 'A3.lrv_no',A4.lrv_noas 'A4.lrv_no',ops_runSheet.createdOnas 'ops_runSheet.createdOn',ops_runSheet.Trackas 'ops_runSheet.Track',ops_runSheet.deliveredByas 'ops_runSheet.deliveredBy',ops_runSheet.operatoras 'ops_runSheet.operator',ops_runSheet.handoffModeas 'ops_runSheet.handoffMode',ops_runSheet.departHandoffas 'ops_runSheet.departHandoff',ops_runSheet.leadLRVas 'ops_runSheet.leadLRV',ops_runSheet.trailLrvas 'ops_runSheet.trailLrv',ops_runSheet.replaceLeadas 'ops_runSheet.replaceLead',ops_runSheet.replaceTrailas 'ops_runSheet.replaceTrail',ops_runSheet.operatoras 'ops_runSheet.operator' FROMops_runSheetops_runSheet LEFT JOINusers_encU ONU.id=ops_runSheet.deliveredByLEFT JOINops_YCCtrackT ONT.id=ops_runSheet.TrackLEFT JOINlrv_listA3 ONA3.id=ops_runSheet.replaceLeadLEFT JOINlrv_listA4 ONA4.id=ops_runSheet.replaceTrailLEFT JOINops_operationModeO ONO.id=ops_runSheet.handoffModeLEFT JOINops_operationModeO2 ONO2.id=ops_runSheet.departHandoff","bindings":[]},{"query":"SELECT DISTINCTidas 'id',Trackas 'Track' FROMops_YCCtrackORDER BYid","bindings":[]},{"query":"SELECT DISTINCTidas 'id',lastnameas 'lastname',firstnameas 'firstname' FROMusers_encWHERE (blocked= :where_1 ANDopsPDC= :where_2 ) ORDER BYlastname","bindings":[{"name":":where_1","value":0,"type":null},{"name":":where_2","value":1,"type":null}]},{"query":"SELECT DISTINCTidas 'id',lastnameas 'lastname',firstnameas 'firstname' FROMusers_encWHERE (blocked= :where_1 ANDopsPDC= :where_2 ) ORDER BYlastname","bindings":[{"name":":where_1","value":0,"type":null},{"name":":where_2","value":1,"type":null}]},{"query":"SELECT DISTINCTidas 'id',Modeas 'Mode' FROMops_operationModeORDER BYid","bindings":[]},{"query":"SELECT DISTINCTidas 'id',Modeas 'Mode' FROMops_operationModeORDER BYid","bindings":[]},{"query":"SELECT DISTINCTidas 'id',lrv_noas 'lrv_no' FROMlrv_listORDER BYid","bindings":[]},{"query":"SELECT DISTINCTidas 'id',lrv_noas 'lrv_no' FROMlrv_listORDER BYid","bindings":[]},{"query":"SELECT DISTINCTidas 'id',lrv_noas 'lrv_no' FROMlrv_listORDER BYid","bindings":[]},{"query":"SELECT DISTINCTidas 'id',lrv_noas 'lrv_no' FROMlrv_listORDER BYid","bindings":[]},{"query":"SELECT DISTINCTops_runSheetops_runSheet.logID as 'dteditor_pkey',users_enc.idas 'id',users_enc.firstnameas 'firstname' FROM ops_runSheet ops_runSheet ops_runSheet ops_runSheet JOINops_runSheetONops_runSheetops_runSheet.logID = ops_runSheet.log JOINusers_encONusers_enc.id=ops_runSheet.operatorIdWHEREops_runSheetops_runSheet.logID IN (:wherein1, :wherein2, :wherein3, :wherein4, :wherein5, :wherein6, :wherein7, :wherein8, :wherein9, :wherein10, :wherein11, :wherein12, :wherein13) ORDER BYfirstnameasc ","bindings":[{"name":":wherein1","value":874,"type":null},{"name":":wherein2","value":875,"type":null},{"name":":wherein13","value":886,"type":null}]}]}"Thank you I have made couple of change and it worked, but, another question. I have another column named delivered BY which is also a list of people so would i have to create another table in database and create another join or could we modify the code to add in the same table.
What were the changes out of interest? I notice you still have an alias, but with itself as the name (
'ops_runSheet ops_runSheet') which would probably still cause an issue.Deliveredby sounds like it might be a left join off one of the tables, but I'm not sure which one?
Allan
If "delivered by" is a list of people I guess this sounds more like an N:M relationship, but could also be 1:N. If it is N:M you would need another link table to resolve that relationship and another Mjoin, too.
If you describe the logical relationships between your tables in more detail I could give you a recommendation.
NO, I totally removed the alias and just used the table name. That helped me, and talking about the "deliveredBy" i do not need it at the moment so its good.
Thanks for your help.
THe code in case you need it
Cool - good to know you got it going!
Allan