How to get the last inserted id? bulk insert in cakephp 3 saveMany


#1

Im implementing bulk insert cakephp data is been successfully inserted to database table
however i cannot get the last inserted id

   $po_data[] = [
                                            'product_id' => $product_id,
                                            'crm_purchase_order_id' => $po_id,
                                            'price' => $price,
                                            'qty' => $quantity,
                                            'crm_quotation_product_id' => $query->id,
                                            'admin_id' => $admin_id,
                                            'additional' => 0, // 0 since it is not an additional quotation
                                            'created_at' => date('Y-m-d H:i:s')
                        ];
                                        
                         //bulk insert
                         $poProducts = TableRegistry::get('CrmPoProducts');
                         $poentities = $poProducts->newEntities($po_data);
                         $poResult = $this->CrmPoProducts->saveMany($poentities, array('atomic' => false));
                                        
                       if( $poResult ){ 
                              $last_po_product_id = $poResult->id;  //this is where the error came from

                              $pom_data[] = [
                                  'crm_quotation_id' => $query->crm_quotation_id,                                                    
                                  'crm_purchase_order_id' => $po_id,
                                  'crm_purchase_order_product_id' => $last_po_product_id, 
                                  'qty' => $quantity,
                                  'created_at' => date('Y-m-d H:i:s')

                                ];
                                            
                               $pom = TableRegistry::get('CrmPurchaseOrderMonitorings');
                               $pomentities = $pom->newEntities($pom_data);
                               $resultInsert = $this->CrmPurchaseOrderMonitorings->saveMany($pomentities, array('atomic' => false));

#2

If I remember correctly, you can use the getLastInsertId() method of a model:

$resultInsert = $this->CrmPurchaseOrderMonitorings->saveMany($pomentities, array('atomic' => false));
$lastInsertId = $this->CrmPurchaseOrderMonitorings->getLastInsertId();

#3

thanks buddy i will try this now


#4

@FinlayDaG33k its not working anymore
Error: Call to a member function getLastInsertId() on array

I think its deprecated as of cake php 3.6
Most likely it will work in cakephp 2


#5

Maybe you could dump the values of $resultInsert and see if it shows any useful data?


#6

Simply get the last entity in the $pomentities array using end($pomentities) and check what it’s id is.


#7

@dakota thank you sir

heres the result:

{“product_id”:1,“crm_purchase_order_id”:6,“price”:1000,“qty”:1,“crm_quotation_product_id”:55,“admin_id”:28,“additional”:“0”,“created_at”:“2019-02-27T08:26:49+00:00”,“id”:71}

i get the last records however how can i get the id of each because i want to save the id of each in another table


#8

Each entity that you save will have its id field set. But since you “want to save the id of each in another table”, it sounds like what you really want is to be saving with associations, whereby Cake will do all the heavy lifting for you.


#9

problem solved i change the structure of my tables