简体   繁体   English

插入,更新,删除 - >规范化和代码点火器

[英]Insert , Update , Delete -> Normalization & Code Igniter

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 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 articles.php

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

}

Model | 型号| articles_model.php articles_model.php

Thomas Clayson already help with insert issue. 托马斯克莱森已经帮助插入问题。

Saving articles and related categories according to normalization 根据规范化保存文章和相关类别

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 感谢帮助

If you need to update articles_categories table with new values, there are some ways to do it. 如果您需要使用新值更新articles_categories表,则有一些方法可以执行此操作。 But my offer is deleting all relations by article id, and then insert them again. 但我的提议是通过文章ID删除所有关系,然后再次插入它们。 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 */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM