44906

Insert , Update , Delete -> Normalization & Code Igniter

Question:

Working with tables and normalization

I have three tables

----------- articles ----------- id int(11) auto_increment title varchar(100) ----------- categories ---------- id int(11) auto_increment title varchar(100) ------------------- articles_categories -------------------- articles_id int(11) categories_id int(11)

I want to save it according to normalization rule to achieve like this

articles_id | categories_id 1 1 1 2 1 3

How can I achieve with code igniter thanks. So far I had already tried like this

View | Create.php

<?php echo form_input('title','','id="title_input"'); ?><br> Category <?php foreach ($categories as $c) { echo '<input type="checkbox" id="categories[]" name="categories[]" value="'.$c['id'].'">'; echo $c['title'].'&nbsp'; } ?> <?php echo form_submit('Submit',"Submit"); echo form_close(); ?>

Controller | articles.php

function insert() { $this->articles_model->save(); }

Model | articles_model.php

Thomas Clayson already help with insert issue.

<a href="https://stackoverflow.com/questions/16733325/saving-articles-and-related-categories-according-to-normalization" rel="nofollow">Saving articles and related categories according to normalization</a>

function insert() { $title = $this->input->post('title'); $data = array('title' => $title); $this->db->insert('articles', $data); $article_id = $this->db->insert_id(); $categories = $this->input->post('categories'); $data = array(); foreach($categories as $category_id) { $data[] = array( 'articles_id' => $article_id, 'categories_id' => $caregory_id ); } $this->db->insert_batch('articles_categories', $data); }

This is delete method

function delete($id) { $this->db->delete('articles',array('id'=>$id)); }

I am now stuck with update method

function update($id) { $id= $this->input->post('id'); $title=$this->input->post('title'); $categories = $_POST['categories']; $data=array( 'title'=>$title, ); $this->db->where('id', $id); $this->db->update('articles',$data); /*can update articles table now */ /* here is the missing idea and what I had done so far*/ $bb = array(); foreach($categories as $categories_id ) { $bb[] = array( 'articles_id' => $id , 'categories_id' =>$categories_id ); } $this->db->update('articles_categories',$bb); /* end of missing idea */ } function select($id) { $query=$this->db->get_where('articles',array('slug'=>$slug)); return $query->row_array(); /*missing idea to retieve categories */ } function select_categories() { /*missing idea to retieve categories */ }

Thanks for help

Answer1:

If you need to update articles_categories table with new values, there are some ways to do it. But my offer is deleting all relations by article id, and then insert them again. It's the best way to update relations table. To do that you can split you insert model;

function insert() { $title = $this->input->post('title'); $data = array('title' => $title); $this->db->insert('articles', $data); $article_id = $this->db->insert_id(); $categories = $this->input->post('categories'); $this->insert_relations($article_id); } function insert_relations($article_id,$categories) { $data = array(); foreach($categories as $category_id) { $data[] = array( 'articles_id' => $article_id, 'categories_id' => $caregory_id ); } $this->db->insert_batch('articles_categories', $data); }

and you need delete model for relations;

function delete_relations($articles_id) { $this->db->delete('articles_categories',array('id'=>$articles_id)); }

and you need to change your missing part like that;

/* here is the missing idea and what I had done so far*/ $this->delete_relations($id); $this->insert_relations($id,$categories); /* end of missing idea */

Recommend

  • Unable to send notifications using firebase functions after update
  • Zend Framework 2, Module Redirect
  • Why does PHP appear to evaluate this condition incorrectly?
  • Scrolling News Ticker Jquery - Issues
  • Contact form problem - I do receive messages, but no contents (blank page)
  • Using extern @class in order to add a category?
  • “A GKScore must specify a leaderboard.”
  • removing last 3 characters on a file (file extension)
  • Extract All Possible Paths from Expression-Tree and evaluate them to hold TRUE
  • XSLT foreach repeating nodes to flat
  • Installing PHP 7 on digitalocean
  • List images(01.png) and descriptions(01.txt) from directory
  • Get specific string
  • How does this usort cmp function actually work?
  • Stop Bash Script if Hive Fails
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • D3 nodes and links from JSON with nested arrays of children
  • FFmpeg Conversion Error
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • what is the difference between the asp.net mvc application and asp.net web application
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • Do I've to free mysql result after storing it?
  • Matrix multiplication with MKL
  • A cron job substitute?
  • json Serialization in asp
  • KeystoneJS: Relationships in Admin UI not updating
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do I configure my settings file to work with unit tests?
  • embed rChart in Markdown
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • To Get the radio button value in ruby on rails
  • java string with new operator and a literal