Hi,
Could please assist me with the following custom clone issue?
I have added the following code to my project:
$e["on_clone"] = array("custom_clone",null,false);
function custom_clone($data)
{
mysql_query("INSERT INTO $table (`HostName`, `Owner`) VALUES ('PC-01', 'Stanislav')");
}
it works fine, but when I try to use something like this:
{
mysql_query("INSERT INTO $table (`HostName`, `Owner`) VALUES ('PC-01', '{$data["params"]["Employee_Name"]}')");
}
I see only 'PC-01' in `HostName` and 'empty value' in `Owner`.
'{$data["params"]["Employee_Name"]}' – Should be the value stored in 'Owner' column of cloned row.
Seems like I did not understand the logic of custom clone function and I need your help.
Thank you.
Hello,
It's not very well documented except a demo code.
function custom_clone($data)
{
$src_id = $data["id"];
$f = $data["params"];
$fields_str = implode(",",$f);
$sql = "INSERT INTO invheader ($fields_str) SELECT $fields_str FROM invheader WHERE id = $src_id";
mysql_query($sql);
}
The params key in $data contains the field names that need to be inserted as duplicate.
It does not have row data.
So if you want row data, you need to first fetch it based on $src_id and then insert like you are doing in code.
In my case there is no data in:
$src_id = $data["id"];
Am I right that in this variable should be id of cloned row?
here is output of:
function custom_clone($data)
{
$src_id = $data["id"];
$f = $data["params"];
$fields_str = implode(",",$f);
\Save variable values to file:
$file = 'd:temppcinv_CustomClone.txt';
$current = file_get_contents($file);
$current .= "Value in $fields_str:" . $fields_str. "rn";
file_put_contents($file, $current);
$current .= "Value in $src_id:" . $src_id . "rn";
file_put_contents($file, $current);
}
___File content:
Value in $fields_str:HostName,Owner
Value in $src_id:
For some reason there is no 'id' in $fields_str, but this column exit in my table. Maybe this is the reason why $src_id has no value?
Ideally, the first column of grid must be unqiue data (usually a pk) which is used to identify the row. You should include it and make it hidden=true to get same result.
And second, in $data["id"] … 'id' represent the first column name. If it's other than id, it should be replaced there.
You can debug the ajax call using firebug and see POST and RESPONSE data for clone call.