插入如果不存在Codeigniter

时间:2014-02-24 15:53:50

标签: mysql codeigniter

我的控制员:

function getFeed()
{
   $feed_url = $this->input->get("url");
   $content = file_get_contents($feed_url);
   $x = new SimpleXmlElement($content);

    foreach($x->channel->item as $entry) {
        $feeds[] = array(
                         'title' => (string)$entry->title,
                         'url'=> (string)$entry->link,
                         'username' => $this->session->userdata('username')                      
                );

        $this->load->model('membership_model');
        $this->membership_model->feeds($feeds);
    }

型号:

function feeds($feeds_data)
{   
        $this->db->insert_batch('feeds', $feeds_data);
}

如果表中不存在行,是否有要插入的函数?我有一个包含4列的表:id,title,url,username。当我点击它时我有一个锚,它调用geFeed函数并将信息插入表中。但我想只在不存在的情况下插入。

5 个答案:

答案 0 :(得分:2)

我遇到了同样的挑战,所以我最终想出了一个可能对你有帮助的功能。

    function safe_update_batch($table_name,$records,$filter_field)
    {
        $filters=array();

        foreach($records as $record)$filters[]=$record[$filter_field];

        $this->db->query("SET SESSION group_concat_max_len=10000000");

        $query=$this->db->select("GROUP_CONCAT($filter_field) AS existing_keys",FALSE)->where_in($filter_field, $filters)->get($table_name);

        $row=$query->row();
        $found_fields=explode(',',$row->existing_keys);         

        $insert_batch=array();
        $update_batch=array();

        foreach($records as $record)
        {
            if(in_array($record[$filter_field],$found_fields))$update_batch[]=$record;
            else $insert_batch[]=$record;
        }

        if(!empty($insert_batch))$this->db->insert_batch($table_name,$insert_batch);
        if(!empty($update_batch))$this->db->update_batch($table_name,$update_batch,$filter_field);      
    }

//样本用法

$this->safe_update_batch('feeds', $feeds_data,'title');

答案 1 :(得分:1)

你可以在你的模特中试试!!

function insertClient($array)
{

        $this->db->from('MyTable');
        $this->db->where('Id', $array['Id']); 
        $query = $this->db->get();
        if($query->num_rows() != 0){
            $data = array(
                        'name'=>$array['name'],
                        'phone'=>$array['phone'],
                        'email'=>$array['email']
                     );

         $this->db->where('Id', $array['Id']);
         $this->db->update('CLIENTS', $data); 
        }else{
            $data = array(
            'name'=>$array['name'],
                        'phone'=>$array['phone'],
                        'email'=>$array['email']
            );
           $this->db->insert('CLIENTS',$data);
        }

}

在控制器中:

$this->site_model->insertClient($_POST);

答案 2 :(得分:0)

遗憾的是,如果您使用的是活动记录类,则INSERT IF NOT EXISTS功能不存在。你可以试试

  • 扩展活动记录类(说起来容易做起来难)
  • 您可以将某些列上的索引设置为UNIQUE,以便MySQL检查它是否已存在
  • 你可以在SELECT之前做某种INSERT来确定记录是否已经存在
  • 对于您需要执行的查询INSERT IF NOT EXISTS执行$this->db->query('INSERT IF NOT EXISTS...')

答案 3 :(得分:0)

function getFeed()
{

    // Load the model up here - otherwise you are loading it multiple times 
    $this->load->model('membership_model');

   $feed_url = $this->input->get("url");
   $content = file_get_contents($feed_url);
   $x = new SimpleXmlElement($content);

    foreach($x->channel->item as $entry) {

    // check if the feed is unique, if true then add to array
    if( $this->membership_model->singleFeedIsUnique($entry) == TRUE  ){     
         $feeds[] = array(
                         'title' => (string)$entry->title,
                         'url'=> (string)$entry->link,
                         'username' => $this->session->userdata('username')); }               

    } //foreach

      // check to make sure we got any feeds with isset()
      // if yes, then add them
      if (isset($feeds)){ $this->membership_model->feeds($feeds); }

} 

答案 4 :(得分:0)

您可以在模型中尝试此操作并让控制器无需更改

function feeds($feeds_data)
{
    $data = array(
        title => $feeds_data[0],
        url => $feeds_data[1],
        username => $feeds_data[2]
        );
    $this->db->select('*');
    $this->db->from('mytable');
    $this->db->where('title',$feeds_data[0]);//you can use another field
    if ($this->db->count_all_results() == 0) {
      $query = $this->db->insert('mytable', $data);//insert data
    } else {
      $query = $this->db->update('mytable', $data, array('title'=>$feeds_data[0]));//update with the condition where title exist
    }
}

你可以检查id,如果有的话,添加数据数组并用它来检查是否存在

相关问题