Inserting new row on edit
Inserting new row on edit
Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:
I'm working on setting up Editor/PHP Library. I'd like to have a history of every change made, thus I have a second SQL table which lists every edit, with timestamp and username. Currently, the PHP library pulls from a MySQL VIEW which returns the newest item from the history table along with the data from the row it is associated with.
So to "edit" the table, I actually want it to just insert a new row into the history table with the relevant information. Could you tell me how this could be done with the library?
Small, related questions:
* Where is the documentation on how the PHP library handles updating the database when receiving an edit from the client?
* Is there any built in history functionality?
This question has accepted answers - jump to:
Answers
Hi,
Have you read over this part of the Editor PHP documentation. It is doing more or less exactly what you want.
Allan
Oh perfect! I hadn't looked through the manual well enough.
Also, what would be the best place to add a check that the user is logged in before performing an INSERT? If I have a variable set to true if logged in, then something like:
I'd prefer something built-in which gives feedback to the client-side, but I couldn't really understand the example in the Security section:
I'll create a new thread for this.
I'm running into a good amount of trouble trying to get this to work. I have this code in
->on()
just as a test:and the code in the
on()
never gets run. I tried preEdit, since it seems postEdit is supposed to be after a successful edit of the database, which I won't have, since the only edit will be the logging. I looked at the debug log, and there's no mention of INSERT or UPDATE. And I think it said preEdit comes before validation functions, so I'd prefer to somehow use those functions.Couple other issues related to this:
* I need to insert into a different logging table depending on which of the three fields is edited. I tried putting ->on() after a Field instead of the Editor, but it gave an error.
* I only want to insert a log row for the field that was edited, but looking at the request sent from the client, it sends the other field values as well when sending the JSON with
action: edit
. Is that changeable?I tried using
submit: 'changed'
in:but it still sends multiple fields in the request:
Looks like the response from the server when trying to edit is basically empty, by the way. Just has the name and version number of the PHP library.
I've added a reply to this one in your other thread.
Have you configured Editor to send the data as a GET request? It will do a POST by default.
If you could give me a link to the page you are working on I can inspect it directly to see what is going on.
With the
preEdit
listener you have there I would expect it to just havetest
in the response body, and then not enact any of the changes to the data. That response, not being valid JSON, should cause Editor on the client-side to show system error.Allan
I did have both server and client as GET, now they're both POST, still not working.
Here's a link:
https://comprehensibleinputwiki.com/ciwlibrary/vidadmin.php
test
doesn't show up, and there's no error.And here's the full server-side code:
Oh - you have:
That disables everything apart from the reading of the data. That would do it . With that option
preEdit
will never fire, since it never gets that far!Allan
Oh wow, I'd forgotten about that line from when I copied the first example. I'm able to now fire the preEdit block.
Now that that works, do you mind helping with the other questions:
Yes it does. The reason is to allow
preEdit
to add extra validation functions if needed (orpreCreate
). If you want an event after the data has been validated use thevalidatedEdit
event mention in the events docs.To confirm my understanding of the issue - you don't want to actually update the main table - you just want to insert that an insert / update / delete was requested into another table, and then presumably you have some other process that will approve those changes and update the main table?
That isn't really a situation that Editor was designed for. The
validatedEdit
event does not provide a way to cancel the action likepreEdit
does.Correct -
on()
is a method of theEditor
class, notField
. However, based on the second question (see below) you can check which field was submitted.Yes - use the
submit
option of theform-options
object (formOptions.main
). Set it tochanged
to submit only the changed value.The open question is how to stop Editor doing its insert / update on the main table, but after the validation... I'll likely need to make
validatedEdit
cancellable for that.Allan
Sort of. When the user performs an "edit", it actually just adds a row to the second table with the new value. When the table is retrieved, it leftjoins the newest corresponding value from the second table. The main table never has to be changed.
Actually, now I realize I can just update the main table (if I switch to using an actual table instead of a VIEW like I'm currently using), and ignore that column of the table later.
Actually, I tried that, as per a previous comment, with no change:
I have three editable fields. Two selects and one checkbox. If I edit one of the selects, it sends data for both selects. If I edit the checkbox, it sends data for all three fields.
Possibly related to this thread.
Well, I actually didn't have it exactly as you suggested.
submit: changed
was ininline()
, but now I've added it where you suggested, but still same behavior.It will submit a value if it thinks the data has changed, which it must do for both
difficulty
andlanguage
there. I'm not such which of the two you have edited though - could you clarify that for me please? Could you also show me the data being loaded into the DataTable, plus the client-side configuration for both DataTables and Editor? Or if you can give me a link to the page, I'll be able to find that information and debug it.Allan
If I edit either one, they both get sent.
Here's the link: https://comprehensibleinputwiki.com/ciwlibrary/vidadmin.php
Oh, maybe because only the label, and not the value, is being sent with the data to the table?
Feel free to experiment with edits. It's not actually set up to write to the database yet.
There is a problem with the label and value that I think we need to address (and I suspect resolving this will also resolve the two data parameters being sent).
Namely, consider this row:
If I click on either difficulty or language it shows not set rather than the correct value:
That is happening because there is no option in the select list that contains the value of the field.
In the data object for that row you have:
However, the difficulty options are:
I.e. there is no value which has Super Beginner. There is a label, but not a value. The item is selected by value.
Do you want to store the number (a foreign key reference?) in the database for difficulty, or the string?
Allan
Only numbers for all editable fields.
In that case, the data coming back from the server should contain that number - e.g.:
(Note that I've used a string, since that is what your options are, but if the data is actually read from the database as a number, you should update the option values to be numbers and not strings).
You will then either want to use a custom renderer to convert the number to a string the user will understand, or include the display text in the data object - e.g.:
Allan
This example shows how it is typically done with joined database data.
Allan
Thank you! I'll give it a try when I get to a computer.
" Note that I've used a string, since that is what your options are, but if the data is actually read from the database as a number, you should update the option values to be numbers and not strings"
So my options are being sent over from the server:
xxxxxx
Actually, I see it's something to do with my default label. Will investigate this myself for now.
Couple other quick questions:
* Is there a way to order the options in the
select
by value, instead of label?* I have a field that is sometimes null. This is breaking SearchPanes, so I'm trying to set the value coming from the server as
-1
when the value isnull
. Usingrender
only changes the label, but leaves the value asnull
. How can I change the value that I send to SearchPanes?No - the label is what the user sees, so there isn't a sort option for the value. That said, I suppose you could include the
value
in the label fields and then use a renderer to just display the label! See the docs here for the full range of options.You'd need to put the renderer on SearchPanes as well. Its a pain I know. That is something I need to look at making easier!
Allan
I think that's what I have:
Do you mean set the options label to the database value, then rendering the option to use the database label instead? Would that not then just sort the select by the rendered label?
I was actually meaning on the client-side, but yes, that should work. That said, I'm not familiar with the PHP syntax used there.
render
should just return a string. I'm not sure what it is returning there to be honest.Regarding the order, thinking about it more, you should actually just be able to use the
order()
method of the options class - set it to order on the value column.Allan
This is what that code produces as a response from the server in the SearchPanes section:
Which seems to go with the documentation about the render function:
What is the client side code that could change the
value
instead of thelabel
?order()
works, thanks!I got the front end working correctly! I couldn't figure out how to fix it with SearchPanes, so instead I replaced null values coming from the database with -1:
Weirdly, having
0
as a value still made it act weird. The request when clicking on0
in the SearchPane would look like this:searchPanes[enabled][0]=0&searchPanes_null[enabled][0]=true
and it would return no results.
If I reran the request with searchPanes_null set to false, it would work correctly.
So I also changed all rows in the database which were
0
to2
, and it works.