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.
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
operator
field 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_runSheet
as the table name, rather than aliasing it asR
please?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
Editor
instance, 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
.id
as 'id',users_enc
.firstname
...' at line 1","data":[],"ipOpts":[],"cancelled":[],"debug":["Editor PHP libraries - version 2.2.2",{"query":"SELECTops_runSheet
.logID
as 'ops_runSheet.logID',ops_runSheet
.id
as 'ops_runSheet.id',U
.username
as 'U.username',T
.Track
as 'T.Track',ops_runSheet
.Direction
as 'ops_runSheet.Direction',ops_runSheet
.Position
as 'ops_runSheet.Position',ops_runSheet
.leadLrv
as 'ops_runSheet.leadLrv',ops_runSheet
.midLrv
as 'ops_runSheet.midLrv',ops_runSheet
.trailLrv
as 'ops_runSheet.trailLrv',ops_runSheet
.sdepartTime
as 'ops_runSheet.sdepartTime',ops_runSheet
.arrivalHandoff
as 'ops_runSheet.arrivalHandoff',O
.Mode
as 'O.Mode',ops_runSheet
.arrivalHandoff
as 'ops_runSheet.arrivalHandoff',ops_runSheet
.adepartTime
as 'ops_runSheet.adepartTime',O2
.Mode
as 'O2.Mode',ops_runSheet
.adepartTime
as 'ops_runSheet.adepartTime',ops_runSheet
.CoupledTrain
as 'ops_runSheet.CoupledTrain',ops_runSheet
.comments
as 'ops_runSheet.comments',ops_runSheet
.changeOffReason
as 'ops_runSheet.changeOffReason',ops_runSheet
.leadMileage
as 'ops_runSheet.leadMileage',ops_runSheet
.midMileage
as 'ops_runSheet.midMileage',ops_runSheet
.trailMileage
as 'ops_runSheet.trailMileage',ops_runSheet
.leadendMileage
as 'ops_runSheet.leadendMileage',ops_runSheet
.midendMileage
as 'ops_runSheet.midendMileage',ops_runSheet
.trailendMileage
as 'ops_runSheet.trailendMileage',A3
.lrv_no
as 'A3.lrv_no',A4
.lrv_no
as 'A4.lrv_no',ops_runSheet
.createdOn
as 'ops_runSheet.createdOn',ops_runSheet
.Track
as 'ops_runSheet.Track',ops_runSheet
.deliveredBy
as 'ops_runSheet.deliveredBy',ops_runSheet
.operator
as 'ops_runSheet.operator',ops_runSheet
.handoffMode
as 'ops_runSheet.handoffMode',ops_runSheet
.departHandoff
as 'ops_runSheet.departHandoff',ops_runSheet
.leadLRV
as 'ops_runSheet.leadLRV',ops_runSheet
.trailLrv
as 'ops_runSheet.trailLrv',ops_runSheet
.replaceLead
as 'ops_runSheet.replaceLead',ops_runSheet
.replaceTrail
as 'ops_runSheet.replaceTrail',ops_runSheet
.operator
as 'ops_runSheet.operator' FROMops_runSheet
ops_runSheet LEFT JOINusers_enc
U ONU
.id
=ops_runSheet
.deliveredBy
LEFT JOINops_YCCtrack
T ONT
.id
=ops_runSheet
.Track
LEFT JOINlrv_list
A3 ONA3
.id
=ops_runSheet
.replaceLead
LEFT JOINlrv_list
A4 ONA4
.id
=ops_runSheet
.replaceTrail
LEFT JOINops_operationMode
O ONO
.id
=ops_runSheet
.handoffMode
LEFT JOINops_operationMode
O2 ONO2
.id
=ops_runSheet
.departHandoff
","bindings":[]},{"query":"SELECT DISTINCTid
as 'id',Track
as 'Track' FROMops_YCCtrack
ORDER BYid
","bindings":[]},{"query":"SELECT DISTINCTid
as 'id',lastname
as 'lastname',firstname
as 'firstname' FROMusers_enc
WHERE (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 DISTINCTid
as 'id',lastname
as 'lastname',firstname
as 'firstname' FROMusers_enc
WHERE (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 DISTINCTid
as 'id',Mode
as 'Mode' FROMops_operationMode
ORDER BYid
","bindings":[]},{"query":"SELECT DISTINCTid
as 'id',Mode
as 'Mode' FROMops_operationMode
ORDER BYid
","bindings":[]},{"query":"SELECT DISTINCTid
as 'id',lrv_no
as 'lrv_no' FROMlrv_list
ORDER BYid
","bindings":[]},{"query":"SELECT DISTINCTid
as 'id',lrv_no
as 'lrv_no' FROMlrv_list
ORDER BYid
","bindings":[]},{"query":"SELECT DISTINCTid
as 'id',lrv_no
as 'lrv_no' FROMlrv_list
ORDER BYid
","bindings":[]},{"query":"SELECT DISTINCTid
as 'id',lrv_no
as 'lrv_no' FROMlrv_list
ORDER BYid
","bindings":[]},{"query":"SELECT DISTINCTops_runSheet
ops_runSheet.logID as 'dteditor_pkey',users_enc
.id
as 'id',users_enc
.firstname
as 'firstname' FROM ops_runSheet ops_runSheet ops_runSheet ops_runSheet JOINops_runSheet
ONops_runSheet
ops_runSheet.logID = ops_runSheet.log JOINusers_enc
ONusers_enc
.id
=ops_runSheet
.operatorId
WHEREops_runSheet
ops_runSheet.logID IN (:wherein1, :wherein2, :wherein3, :wherein4, :wherein5, :wherein6, :wherein7, :wherein8, :wherein9, :wherein10, :wherein11, :wherein12, :wherein13) ORDER BYfirstname
asc ","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