Is there any way copy a row to another table and after delete?

QuestionsIs there any way copy a row to another table and after delete?
Tasagore Seibei asked 2 years ago

Hi

I don’t want to delete any data but I want to be able to archive rows in a easy way (not editing a row).

The ideal behavior should be click in button (like delete, add…), ask for the reason and after ok set a value in field to know it’s archived and the reason.

Is there any way to do something like that?

Tasagore Seibei replied 2 years ago

Sorry for the topic, is not correct, I don’t need to move that data to another table, just mark it as archived and reason.

3 Answers
Best Answer
Tasagore Seibei answered 2 years ago

Hi Abu You say third parameter to true to skip the default delete operation, but in the example I see this: \”If you pass last argument as true, functions will act as a filter and insert/update in ->table will be performed by grid after your function. If last argument is set to false, only your function handler will be executed and grid\’s internal implementation will be ignored.\” So I guess the third parameter should be false since I don\’t want to delete the register, just update a field, isn\’t it? In same documentation I can see that \”in the on_delete event only receive ID of grid\” so I guess this means that $data will have only the ID field of the row, right? (in my table the index field is ID) So the function should be like this: function delete_client($data) { global $grid; $grid->execute_query(\”UPDATE mytable set ARCHIVED=1 where ID={$data[\”params\”[\”ID\”]}\”); } Is that right?

Tasagore Seibei replied 2 years ago

unable to delete the post, sorry for the format, was typed in text mode 🙁

Abu Ghufran Staff answered 2 years ago

You need to use on_delete event handler (with thrid param to true to skip default delete operation) and write your custom code for soft delete by making a field like (is_deleted to 1).

And in grid select_command use this condition like SELECT … FROM table WHERE is_deleted=0.

Ref: https://www.gridphp.com/docs/grid-events/

_________________________
Abu Ghufran - Dev Team
Grid 4 PHP Framework
 
Abu Ghufran Staff replied 2 years ago

PS: Custom events are supported in full version.

Tasagore Seibei answered 2 years ago

Hi Abu, thanks for the fast answer.

I’m a bit confused, you say that third parameter should be true but this will delete the register so I guess should be false, isn’t it?

Also I’ve read that in the delete events only the ID parameter will be passed, so the code should be something like this (in mytable the index field name is ID):

function add_client($data)

{

global $grid; $grid->execute_query(“UPDATE mytable SET archived=1 where ID={$data[“params”][“ID”]}'”);

}

Should it be right?

Abu Ghufran Staff replied 2 years ago

Yes, you are correct. Third param should be false in your case.
Code looks fine.

PS: Make sure you connect on_delete event with your function, not with on_insert.
Function name in your example (add_client) in creating confusion. Should be del_client or something.

Your Answer

18 + 11 =

Login with your Social Id:

OR, enter

Attach code here and paste link in question.
Attach screenshot here and paste link in question.



How useful was this discussion?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate it.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?