spotorm / spot2

Spot v2.x DataMapper built on top of Doctrine's Database Abstraction Layer
http://phpdatamapper.com
BSD 3-Clause "New" or "Revised" License
601 stars 101 forks source link

REPLACE queries #241

Closed leereichardt closed 7 years ago

leereichardt commented 7 years ago

I can't for the life of me figure this out, but is there a mapper function that does a REPLACE INTO query?

FlipEverything commented 7 years ago

I don't think so.

willemwollebrants commented 7 years ago

I don't think so either. As far as I know, REPLACE INTO is a mysql-specific extension so it wouldn't be portable (it's not standard SQL)

FlipEverything commented 7 years ago

I agree. Spot should remain portable.

leereichardt commented 7 years ago

Thanks. I reverted to creating a custom mapper and building my own way. One thing I found, and it took me a while to realise, is that the ->query function is for select only. It might be good to include a simple query function for doing these "other" queries. Once that doesn't return an entity?

FlipEverything commented 7 years ago

I think you can do that. It was merged a while ago: #180 ex.: $mapper->exec($sql); Or you can extend the Mapper class and implement a custom replace method here to satisfy your needs.

leereichardt commented 7 years ago

Ah of course! It was a late night alright! Thanks for the help!

vlucas commented 7 years ago

There actually is an upsert function that will attempt to INSERT the record, and then will attempt to UPDATE it instead on duplicate key failure. It is not going to be as efficient as a native REPLACE INTO, but it is fully portable across database drivers.

https://github.com/spotorm/spot2/blob/master/lib/Mapper.php#L881