Clone record to a second table

QuestionsClone record to a second table
Matt asked 7 months ago

What I would like to be able to do is have one grid shown on the top of the screen loading original source data from Table A. Then have a second Table B as a grid shown below that so that if a user clicks the clone record button on Table A it is inserted to Table B for them to make any changes to. How would I go about doing this and is it possible to make Table A non editable yet still have the Clone option to copy to Table B?

 

Thanks in Advance!

5 Answers
Abu Ghufran Staff answered 7 months ago

You can use on_clone event handler and write your own code to insert in other table.

https://www.phpgrid.org/demo/demos/editing/clone-row.phps

_________________________
Abu Ghufran - Dev Team
Grid 4 PHP Framework
 
Matt answered 7 months ago

Thank you. That works perfect!

The only other things I can’t quite seem to get working on clone handler are to set the Created date in the table it’s inserted to along with Created_By as the persons name who clicked the clone button. The persons name would be a preset PHP variable pulled from their profile. The on clone event insert query I did change to a plain SQL Select/Insert because I didn’t want to insert all of the “Cloned” column values, just a subset.

So for example the SQL I changed it to be like this.

$sql = “INSERT INTO Books (Title, ISBN, Author, Published, Publisher, Category, Type) SELECT Title, ISBN, Author, Published, Publisher, Category, Type) FROM Inventory WHERE id = $src_id”;

 

Id like to have Created set to current date the clone button was pressed and Created_By set to $Created_By

Is there an easy way to do this either within that on_clone event handler or immediately after the insert is made?

Thanks!

Abu Ghufran Staff answered 7 months ago

You can use similar query with bold modification.

$sql = “INSERT INTO Books (Title, ISBN, Author, Published, Publisher, Category, Type, Created_date, Created_by) SELECT Title, ISBN, Author, Published, Publisher, Category, Type, NOW() as Created_date, ‘$Created_By’ as Created_By FROM Inventory WHERE id = $src_id”;

_________________________
Abu Ghufran - Dev Team
Grid 4 PHP Framework
 
Mat answered 7 months ago

This is an SQL Server table I am trying to get this to work for. When I put the variable name inside quotations and refresh the grid nothing is displayed but a blank page. If I put the variable between single quotes the grid loads but the variable value is not passed to the insert query. It’s just an empty value. I’m sure it’s something simple I am doing wrong here.

Abu Ghufran Staff answered 7 months ago

Using single quote should work. Make sure the variable $Created_By contains value. You can check by hardcoding it. e.g.

$Created_By = “test”;

$sql = “INSERT INTO Books (Title, ISBN, Author, Published, Publisher, Category, Type, Created_date, Created_by) SELECT Title, ISBN, Author, Published, Publisher, Category, Type, NOW() as Created_date, ‘$Created_By’ as Created_By FROM Inventory WHERE id = $src_id”;

_________________________
Abu Ghufran - Dev Team
Grid 4 PHP Framework
 
Your Answer

17 + 8 =

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 5 / 5. Vote count: 1

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?