Cakephp 3 saveMany return false on save

#1
                $quote_id = (int) $this->request->getData('quotation-'.$i); 
                $selectQuery = $this->CrmQuotationProducts->find('all',[
                ])->where(['crm_quotation_id IN' => $quote_id ]);
                    $po_data = [];
                    foreach($selectQuery as $index => $query){
                        $quantity= $this->request->getData('quantity-'.$i);
                        
                        $po_data[] =  array(
                            'product_id' => $product_id,
                            'crm_purchase_order_id' => $po_id,
                            'price' => $price,
                            'qty' => $quantity,
                            'crm_quotation_product_id' => $query->id, //from crm_quotation_product - id
                            'crm_quotation_id' => $query->crm_quotation_id, //from crm_quotation_product - crm_quotation_id
                            'admin_id' => $admin_id,
                            'additional' => 2,
                            'created_at' => $created_at
                        );
                        
                        $poProducts = TableRegistry::get('CrmPoProducts');
                        $entities = $poProducts->newEntities($po_data);
                        $result = $poProducts->saveMany($entities);
                       
                        echo json_encode($result);

                        
                    }
0 Likes

#2

Have you looked at the errors in the entities?

1 Like

#3

its already solved thanks for your help
its in the validation error “incorrect date format”

0 Likes

#4

Goodday Zuluru 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));
0 Likes

#5

You have “saved many”, the last ID isn’t going to be that useful. The IDs for each entity should be in the individual entities saved now, though. If you’re only saving one entity, use save, not saveMany.

Also, looking at your original code more closely, it looks like you’re building up an array of entities, but saving the whole batch every time through the loop? That should actually work okay because each entity remembers whether it needs to be saved and all but the newest will be skipped each time, but your code would be better if it saved once after the loop was done.

And lastly, if your table associations are set up correctly, you shouldn’t even need to get the last saved ID, you can put all the data into a single, nested array, create both the product and purchase order entities at once, and save everything in one big blast at the end. See here for details of this.

0 Likes