Welcome Guest, Not a member yet? Register   Sign In
How do you insert data into multiple different tables at a time?
#1

I am getting my head around CodeIgniter and SQL at the same time and I am currently wondering how do I add data (or indeed, search for data) that is stored in multiple tables?

For example, if I had a 'Users' table and a 'Posts' table, how do I accept that data and store it in the correct tables using CodeIgniter? Also, how would this work for something more complicated that uses more than two tables?

Additionally, how does this apply to selecting data from multiple tables as well?

Id really appreciate some pointers on this.
Reply
#2

This is from a blog that I' am working on.

You can see that I get an instance to all 3 models.

PHP Code:
    // -------------------------------------------------------------------

    /**
     * post ()
     * -------------------------------------------------------------------
     *
     * @param int $id
     */
    
public function post(int $id 0)
    {
        
$posts      = new PostModel($this->request);
        
$categories = new CategoryModel();
        
$comments   = new CommentModel($this->request);

        
$data = [
            
'posts'      => $posts->getPost($id),
            
'comments'   => $comments->getComments($id),
            
'categories' => $categories->getTopCategories(),
            
'titles'     => $categories->getCategoryTitles($id),
            
'title'      => 'Blog Post',
            
'typography' => Services::typography(),
        ];

        
$data['pageHeading'] = $data['titles']['title'];
        
$data['subHeading']  $data['posts']['sub_title'];

        
// Make all variables global to all views...
        
echo view('Insitefx\Blog\Views\view_data'$data);
        echo 
view('Insitefx\Blog\Views\post\index');
    } 

In the $data section you can see how I' am reading from all 3 tables PostModel, CategoryModel and CommentModel.

You should have a separate Model for each of your tables.

You would do everything like above for Inserting, Updating and Editing.

Hope that will get you started, would be best to read the User Guide on Working with Databases and Modeling Data.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Thank you so much for helping me out with this. It actually seems quite simple. I have a tendency to make things seem harder than they actually are!
Reply
#4

Best thing is to take a weekend sit down with a beer or whatever and read the complete CodeIgniter User Guide.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB