Create Row With Custom Primary Key
Create Row With Custom Primary Key
Greetings
I use custom primary key, named key_id
instead of the id
column.
I use custom function to create this unique key_id, and using setFormatter on the field for that, while in the javascript side, I sent the key_id
as a hidden field with value of null or false, just to make sure it will use the setFormatter function.
Example:
$e = Editor::inst($db, 'customers', 'key_id')
->fields([
Field::inst('customers.key_id')->setFormatter(function ($data) {
if($data){
//Not Modifying the KeyId in case of editting.
return $data;
}else{
//Generating New Id for Newly created Row, which has $data of false or null;
return SomeSpecialFunctionForRandomKeyId();
}
}),
Field::inst('customers.name'),
It is creating it fine, but the problem is, when I listen for the json of the creating request in javascript, I get empty data array. (I'm expecting to get the newly created row in the data array).
When I truned on debug mode for sql queries, I got two of them:
1- One for insertion, which is doing well.
2- Second one for selecting the created row, which bind the value of (ID OF CREATED ROW) to the key_id
primary key column. That's why I get empty array I guess.
Any mistakes in my script or any suggestions ?
This question has an accepted answers - jump to answer
Answers
This sounds like the correct place to be looking. But I'm not quite clear why it isn't working! Could you show me the JSON returned from the create request, so I can see the the SQL and parameters?
Also, one thing to try - set the primary key to be
customer.key_id
for the third parameter in the constructor. I think that might actually address the issue.Thanks,
Allan
This is the json:
Anyway, when I tried your solution of adding table name to primary key in constructor, IT WORKED!
Thank you.