0

I have a very simple MySQL table and I need to execute this query:

INSERT INTO modx_good (uuid,name) VALUES ('1','name')
  ON DUPLICATE KEY UPDATE uuid='1', name='name';

My table is modx_good:

`id` int(11) NOT NULL AUTO_INCREMENT,
`uuid` varchar(100) NOT NULL,
`name` varchar(100) NOT NULL,

And I need to run this using features of modx and xpdo.

I can insert data by

$modGood = $this->modx->newObject('Good');
$modGood->fromArray([ 
  'uuid' => 'a1', 
  'name' => 'name1'
]);
$modGood->save();

But how can I add the ON DUPLICATE KEY condition that I had in the raw query?

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Asylzat
  • 197
  • 5
  • 17
  • But the key cannot be duplicated here, since you don't give a key yourself... – Willem Van Onsem May 23 '17 at 12:52
  • uuid is uniq, i tested query, works fine, if there is a same uuid it updates all other fields not creating new input. I found this query in google and it seems to work just fine, my problem is i have to make the same using modx xpdo. on duplicate - https://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html – Asylzat May 23 '17 at 12:59
  • To my knowledge xPDO doesn't support `ON DUPLICATE KEY`, so you wont have much luck doing this through the object related methods. You could craft the query yourself and fire it off as prepared statement with $modx->query() though. See https://docs.modx.com/xpdo/2.x/class-reference/xpdo/xpdo.query – Mark Hamstra Aug 09 '17 at 16:28

0 Answers0