CodeIgniter Forums
Inserting data into multiple tables - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Inserting data into multiple tables (/showthread.php?tid=68141)



Inserting data into multiple tables - Junie - 05-31-2017

Hello,

In basic sql command to insert data into multiple table with foreign key relation the last inserted id is helpful. In codeigniter how can I insert data into  multiple table with foreign key relation. I'm reading at some helpful tutorials like using transactions. Can someone teach me how to implement in codeigniter or any code snippets that can share in order to help me. I'm still learning this framework.

Thanks Smile


RE: Inserting data into multiple tables - shaser - 06-01-2017

with codeigniter you have to create as many array as table you want to insert the data

this will be in your model

function addProductCategory($aProductCategory){
$this->db->insert('tbl_products_type',$aProductCategory);
}

this in in your controller

function insertProductCategory(){

$productCategory = array('product_type_name' => $this->input->post('txt_product_category'));

$this->mod_Product_Category->addProductCategory($productCategory);

}

you get the value from the view


RE: Inserting data into multiple tables - Paradinight - 06-01-2017

(05-31-2017, 08:53 PM)Junie Wrote: Hello,

In basic sql command to insert data into multiple table with foreign key relation the last inserted id is helpful. In codeigniter how can I insert data into  multiple table with foreign key relation. I'm reading at some helpful tutorials like using transactions. Can someone teach me how to implement in codeigniter or any code snippets that can share in order to help me. I'm still learning this framework.

Thanks Smile

Have you read https://www.codeigniter.com/user_guide/database/transactions.html ?


RE: Inserting data into multiple tables - Junie - 06-01-2017

(06-01-2017, 12:02 PM)Paradinight Wrote:
(05-31-2017, 08:53 PM)Junie Wrote: Hello,

In basic sql command to insert data into multiple table with foreign key relation the last inserted id is helpful. In codeigniter how can I insert data into  multiple table with foreign key relation. I'm reading at some helpful tutorials like using transactions. Can someone teach me how to implement in codeigniter or any code snippets that can share in order to help me. I'm still learning this framework.

Thanks Smile

Have you read https://www.codeigniter.com/user_guide/database/transactions.html ?

I already read this. What Im trying to emphasize is does transaction is the way to insert data from a foreign key constraint? Or theirs other way.


RE: Inserting data into multiple tables - natanfelles - 06-01-2017

Perhaps it can help you: CodeIgniter Database Helper to Add and Drop Foreign Keys