简体   繁体   中英

Mass Insert Queries Into One

I've written this message to send a new personal message to a user however I'm trying to find out how I can redo this a little bit because I've been told I shouldn't be doing a query inside of a loop because that could accumulate to hundreds of queries and instead do one query at a time. I am using CodeIgniter's database class and more specifically the Active Record class of it.

function sendMessage($recipients, $subject, $message, $sender, $bcc = array())
{
    // Check args
    if(!is_array($recipients)) { throw new Exception('Non-array $recipients provided to sendMessage()'); }
    if(!is_string($subject)) { throw new Exception('Non-string $subject provided to sendMessage()'); }
    if(!is_string($message)) { throw new Exception('Non-string $message provided to sendMessage()'); }
    if(!is_numeric($sender)) { throw new Exception('Non-numeric $userID provided to sendMessage()'); }
    if(!is_array($bcc)) { throw new Exception('Non-array $bcc provided to sendMessage()'); }

    $this->db->set('subject', $subject); 
    $this->db->set('senderID', $sender); 
    $this->db->set('message', $message); 
    $this->db->insert('usersPersonalMessages');
    if ($this->db->affected_rows() == 1)
    {
        $insertID = $this->db->insert_id();
        foreach ($recipients as $recipientID)
        {
            $this->db->set('userID', $recipientID); 
            $this->db->set('usersPersonalMessagesID', $insertID); 
            $this->db->insert('usersPersonalMessagesRecipients');
            if ($this->db->affected_rows() == count($recipients)) 
            {
                continue;
            }
        }

        if (isset($bcc) && (!empty($bcc)))
        {
            foreach ($bcc AS $bccID)
            {
                $this->db->set('userID', $bccID); 
                $this->db->set('usersPersonalMessagesID', $insertID); 
                $this->db->set('type', 2); 
                $this->db->insert('usersPersonalMessagesRecipients'); 
            }
            if ($this->db->affected_rows() == count($bcc)) 
            {
                continue;
            }                
        }
        continue;
    }  
    return TRUE;
}

EDIT: Any additional ideas because I already have a array called $recipients.

You don't want to do this . Please try with batch insertion .This will insert query at once , so db interaction done only one times

As of codeigniter docs

$data = array(
   array(
      'title' => 'My title' ,
      'name' => 'My Name' ,
      'date' => 'My date'
   ),
   array(
      'title' => 'Another title' ,
      'name' => 'Another Name' ,
      'date' => 'Another date'
   )
);

$this->db->insert_batch('mytable', $data);

// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date')

Refer this below link http://codeigniter.com/user_guide/database/active_record.html#insert

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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