First you SELECTto know whether you should INSERTor UPDATE, for example:
if (
$db->fetchOne(
'SELECT COUNT(1) FROM parts WHERE article_number = ?',
$p->article_number
)
) {
$db->update(
'parts',
$p->to_array(),
$db->quoteInto('article_number = ?', $p->article_number)
);
}
else {
$db->insert('parts', $p->to_array());
}
Re Milen comment: wonderful point! With PostgreSQL's default transaction isolation level ( READ COMMITTED), it is possible that another process inserts (and commits) the "your" part after yours SELECT, but before yours INSERT.
, , - ( , ). SET TRANSACTION ISOLATION LEVEL SERIALIZABLE. . .