Create Row With Custom Primary Key

Create Row With Custom Primary Key

HassanDomeDeneaHassanDomeDenea Posts: 29Questions: 10Answers: 0

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

  • allanallan Posts: 61,726Questions: 1Answers: 10,110 Site admin
    Answer ✓

    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.

    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

  • HassanDomeDeneaHassanDomeDenea Posts: 29Questions: 10Answers: 0

    This is the json:

    {
    "data":[],
    "debug":[
       {"query":"INSERT INTO  customers  ( created_at, created_by, key, key_id, name ) 
                      VALUES (  :created_at,  :created_by,  :key,  :key_id,  :name )",
        "bindings":[{"name":":created_at","value":"2020-02-28 05:37:49","type":null}, 
                         {"name":":created_by","value":"sAhmed1","type":null}, 
                         {"name":":key","value":"sAhmed","type":null}, 
                         {"name":":key_id","value":"1_sAhmed-37","type":null}, 
                         {"name":":name","value":"\u0632\u0628\u0648\u0646 
                                                                  \u062c\u062f\u064a\u062f","type":null}]
       },
       {"query":"SELECT  key_id as 'key_id', customers.created_at as 
                       'customers.created_at', customers.created_by as 'customers.created_by', 
                        customers.updated_at as 'customers.updated_at', customers.updated_by 
                       as 'customers.updated_by', customers.deleted_at as 
                       'customers.deleted_at', customers.deleted_by as 'customers.deleted_by', 
                       customers.key as 'customers.key', customers.key_id as 
                       'customers.key_id', customers.name as 'customers.name', 
                       customers.phone as 'customers.phone', customers.address as 
                       'customers.address', customers.description as 'customers.description' 
                       FROM  customers WHERE customers.key = :where_0 AND  key_id = 
                       :where_1 ",
        "bindings":[{"name":":where_0","value":"sAhmed","type":null}, 
                          {"name":":where_1","value":"37","type":null}]
       }
    ]
    }
    

    Anyway, when I tried your solution of adding table name to primary key in constructor, IT WORKED!
    Thank you.

This discussion has been closed.