Before Update Data

kiksekage

New member
Hi,

How can I change data from the array that is passed in a "before update" hook?

Thanks!


[pre]
You are using DaDaBIK version 8.1-Lerici enterprise
PHP Version: 7.0.24
mysql version: 5.6.37
Web server: Apache
Client: Mozilla/5.0 (Windows NT 10.0; Win64; x64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063
[/pre]
 

kiksekage

New member
Ugh. I tried adding a custom update to change the value but nothing happens.

I don't see any errors and I tested the query outside of the script.

[pre]
global $conn, $current_user;

$sql = "UPDATE DataTable SET UserName = :user WHERE datatable_id = :pk;";
$stmt = $conn->prepare($sql);
$stmt->bindValue(":user", $current_user, PDO::pARAM_STR);
$stmt->bindValue(":pk", $pk, PDO::pARAM_INT);
$stmt->execute();
[/pre]

What am I doing wrong? I'd rather just change the outgoing data in the array but I thought this would at least give me something that worked, for now.
 

eugenio

Administrator
Staff member
Hello,
I am not sure what is your final goal: changing the values of the table that the user is modifying ? Or is it another table?

In your function it seems the variable $pk is not set.

Best,
 

kiksekage

New member
Hi,

Yes. I want to modify a couple of fields that the user is not allowed to change.

$pk is the primary key being passed to the function.

function dadabik_after_datatable_update($pk)

I changed to after because otherwise, the values were overwritten. I need to know the values that went into the system, so I had to do a SELECT query to get that row. I would prefer to change the values before DaDaBIK's update, instead of having to do my own update after DaDaBIK.

In any case, I have it working, at last but I have another question. When I click "Save" in the edit screen, DaDaBIK returns to the table list instead of staying on the edit screen. Do I need to return a value or something from my "after update" function? I want it to stay on that screen.

Thank you!
 

eugenio

Administrator
Staff member
Hello,
oh ok yes, exactly, the problem was that the regular update overwrites your custom update unless you use "after".

About the second problem, this is strange, in your edit form you should have three buttons: save, back and save+back. Save just stays in the current form (see the online demo for the correct behaviour), in your case this doesn't happen?

The only reason I can think is that DaDaBIK does not find anymore the record because you are changing the value of the primary key, which is the only way it has to recognize the record.
 

kiksekage

New member
Ah, I see!

The record gets changed, so that it is no longer in the view. Once the data is entered, I want it to become read-only, so I move it to a view where users can't edit it.

DaDaBIK is doing the most sensible thing, by not letting the edit screen stay open. Thanks for letting me know!

I wonder if there's any way to redirect the user to the "Details" page for that row, using the other view. If the PK is the same, is that possible? It's kind of a crazy idea but you never know. :)
 

eugenio

Administrator
Staff member
Ok, so that was the reason of the problem. There isn't any built-in way to redirect to another table/view, but if you know some PHP you could play a bit with index.php, look for the line containing:

case "update":

here you have all the code executed by DaDaBIK during an update operation. Some lines later, after the first:

complete_trans_db();

you have the code that handles the redirection after update. There you could add an if statement like:

if ($table_name === 'your table'){
header('location:'.$site_url.$dadabik_main_file.'?tablename='.urlencode('new_table_name').'&function=edit.................&where_field=....&where_value=...)));
}

using as "where_value" (that's the value of the PK used by dadabik) the value you received from the user in _POST

That's just a hint for an hack but it can work!

Best,
 
Top