AccelerationNet / wp-db-table-editor

A worpdress plugin that allows direct excel-like editing of a database table in your wordpress database.
BSD 3-Clause "New" or "Revised" License
38 stars 16 forks source link

Custom Save Hook #25

Closed akela92 closed 7 years ago

akela92 commented 7 years ago

Hello everyone, I've open before an Issue about this a few weeks ago, but I haven't found a solution yet. I am using a join query from 3 different tables and therefore the edited values are not stored in the DB. I've tried to solve it with the default dbte_save_cb by making all the values except from the one that I am interested non editable. This solution didn't work as you can imagine. The second solution that bobbysmith007 gave me was to create a custom hook for the save function. I've tried that also but I don't really understand the functionality of the example xxx_contacts_save function. Maybe you could answer a few questions for me:

I've also tried to debug the original dbte_save_cb with echos but nothing gets printed.

Thanks again for this great plugin and I hope that someone can help me solve this issue.

bobbysmith007 commented 7 years ago

Sorry that the example was not enough to get you going. It was culled rather mercilessly and carelessly from a project where it works, but it looks like I may have not gotten everything just exactly so. Also contact form db has a strange database layout, so it is not as clear as it could be.

I've tried to solve it with the default dbte_save_cb by making all the values except from the one that I am interested non editable. This solution didn't work as you can imagine.

Why did that solution not work, I have done this in the past without trouble?

Why do you declare $columns twice?

Because I too make typos and this was undebugged code by the time i pulled it out of the parent project ;)

I've also tried to debug the original dbte_save_cb with echos but nothing gets printed. Which values stores exactly the $args variable? 'table', 'columns',...?

You probably need to var_dump $args to get an exact understanding of what is passed - it has been a while since I worked on it. Look in the ajax response to clicking the "save" button to see any messages echo/var_dump'ed out. (Use the developer tools in Firefox or Chrome).

Looking at line 420 of db-table-editor.php you can see the save callback being called.

// args generation for user callback
 $data = Array('table'=>$cur,  // dbte instance
                      'update'=>$up, // an array of columns=>values to put in the database
                      'columns'=>$cols, // a list of table columns
                      'indexes'=>$idxs[$ridx] //a list of the modified data indexes for this row,
                      'id'=>$id, // the primary key of this row
// if this row has no primary key this will be true (we need to insert 
// and return the primary key by modifying "id" in the args array
                      'isinsert'=>$isinsert); 

I really get lost when I come to the $vals, $k, $v variables. What are each of them?

$vals was unset in the example code (I am fixing it), $k is the column name, $v is the value supposed to be assigned to it

Let me know if I have missed anything

bobbysmith007 commented 7 years ago

Custom save hook example: https://github.com/AccelerationNet/wp-db-table-editor/blob/master/examples/cf7dbsubmit_integration.php#L97

Where custom save hooks are called: https://github.com/AccelerationNet/wp-db-table-editor/blob/master/db-table-editor.php#L418

akela92 commented 7 years ago

I don't know why the first solution didn't work. My array looks something like this:

'id_column'=>'answer_recommendation_id',
'noedit_columns' => 'quiz_name, question_order, question_name, question_id,
            answer_recommendation_id, answer',
'sql'=>'SELECT quiz_name, question_order, question_name, answer,
            recommendation, user_answer_recommendations.question_id,
            answer_recommendation_id
            FROM user_answer_recommendations INNER JOIN questions
            on user_answer_recommendations.question_id=questions.question_id
            INNER JOIN quizzes ON questions.quiz_id= quizzes.quiz_id'

I am going to try to create a custom save hook with the new code you uploaded, but if you can tell me if you see anything wrong that would be great.

Thanks for answering so fast, and again, great plugin

akela92 commented 7 years ago

I've been debugging for quite a while now and the error I found in the debug.log is the following.

WordPress database error Unknown column 'question_order' in 'field list' for query UPDATE 
`wp431_ardour_rs_user_answer_recommendations` SET `question_order` = '1', 
`question_name` = 'Das Beratungsunternehmen sollte einen sehr bekannten Brand haben.', 
`answer` = 'sehr unwichtig', `recommendation` = 'asdasd', `question_id` = '14' 
WHERE `answer_recommendation_id` = '46' made by do_action('wp_ajax_dbte_save'), call_user_func_array, dbte_save_cb, QM_DB->query

I still haven't found a solution to this error. I've checked several forums and this is the solution most of them give. This is the point where I get stuck, is there an easy solution to my problem?

On the other hand I checked the code for the custom save hook you uploaded and I still don't understand from where you get the values for $up, $cols, $idxs and $id.

bobbysmith007 commented 7 years ago

I don't know why the first solution didn't work. My array looks something like this:

I see in your debug message that all the noedit_columns have a space in them. I have fixed the code to trim, the columns (You can fix it without upgrading by removing any space fromnoedit_columns). I have updated the code on github to trim the column names.

On the other hand I checked the code for the custom save hook you uploaded and I still don't understand from where you get the values for $up, $cols, $idxs and $id.

The javascript posts json values that are then processed in the ajax handler. Once these are processed to make an "update array" (an array of columns to their new values) we call the user callback for the data.

akela92 commented 7 years ago

Good morning, removing the spaces worked perfectly!!!! Thanks a lot for the help

bobbysmith007 commented 7 years ago

Glad you got it squared away!