Hamid Ghorashi Mon Aug 05 03:50:26 -0400 2013

Subject: Prevent inserting duplicate entry

Is there any way to ignore inserting duplicate entry on a table? How can I apply "INSERT IGNORE" to Activerecord if it has not been provided?


Nuita S Tue Aug 13 08:46:11 -0400 2013

Hi Hamid,

I think you can do it with a raw query. For example:

User::query(
'INSERT IGNORE INTO users (id, name, age) VALUES (?,?,?) ',
array(18, 'John', 41)
);

Same thing for ON DUPLICATE KEY:

User::query(
'INSERT INTO users (id, name, age) VALUES (?,?,?) ON DUPLICATE KEY UPDATE name=?, age=?',
array(18, 'John', 41, 'John', 41)
);

(1-1/1)