CodeIgniter Forums

Full Version: Make a new query builder inside a model afterCreate callback
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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 !