• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Make a new query builder inside a model afterCreate callback

#1
Hi guys. i'm having troubles with the models callback...

in my ProductModel.php, i've got this afterCreate callback

PHP Code:
public function createCatalogPivots($data){
        if (! isset(
$data['data']['catalog_pages_ids'])){
            return 
$data;
        }

        
$product_id $data['result']->connID->insert_id;
        
        foreach (
$data['data']['catalog_pages_ids'] as $catalog_page_id){

            
$productPivotModel = new CatalogPageProductModel();

            
$insert = [
                
'catalog_page_id' => $catalog_page_id,
                
'product_id' => $product_id,
            ];

            
$productPivotModel->insert($insert);
        }

        unset(
$data['data']['catalog_pages_ids']);

        
//var_dump($data);die;

        
return $data;
    } 

when i create a product, i reference many categories linked with this product in a category_product table (HABTM).

and when i send 1 or many "catalog_pages_ids" my $data['result']->connId reference my last pivot insert and not my single product created.

so the problem is that the $product_model->getInsertID() on my Product controller return the last insert id of my pivot table and not my product table.

is that the normal behavior ? maybe i'm missing something or doing my callback's wrong.

thanks guys !
Reply


Messages In This Thread
Make a new query builder inside a model afterCreate callback - by keulu - 04-11-2019, 08:26 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.