Paste if Codeigniter Does Not Exist

my controller:

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);
    }

Model:

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

Is there a function to insert if only the row does not exist in the table? I have a table with 4 columns: id, title, url, username. I have an anchor, when I click on it, it calls the geFeed function and inserts the information into the table. But I want to insert only if it does not exist.

+3
source share
5 answers

I had the same problem, so I eventually came up with a function that might be useful to you.

    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);      
    }

// usage example

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

You can try it in your model!

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);
        }

}

In the controller:

$this->site_model->insertClient($_POST);
+1
source

, , INSERT IF NOT EXISTS .

  • ( , )
  • You can set indexes on specific columns as UNIQUE so MySQL checks to see if it already exists
  • You can do something SELECTbefore INSERTdetermining that a record already exists.
  • For queries where you need to INSERT IF NOT EXISTSdo do$this->db->query('INSERT IF NOT EXISTS...')
0
source
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); }

} 
0
source

You can try this on your model and leave the controller unchanged.

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
    }
}

you can check the identifier, if you have one, add to the data array and use it to check for availability

0
source

All Articles