Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find or create (upsert) functionality in Doctrine 2

Does Doctrine 2 have upsert functionality built in? It doesn't seem to, but I wasn't able to find a definitive yes-or-no answer.

If it does, I would of course be interested to see an example and/or some documentation.

like image 502
Jason Swett Avatar asked Oct 15 '12 15:10

Jason Swett


2 Answers

I believe I found the answer. As of today (10/15/2012), there's an open "add upsert support" issue for Doctrine. I assume that this ticket wouldn't still be open if Doctrine 2 did have upsert support, so I guess there's my definitive answer.

like image 176
Jason Swett Avatar answered Oct 03 '22 20:10

Jason Swett


Upsert is already present in Doctrine.

Using the query builder, you have to set findAndUpdate() and returnNew() if you want to return the document. Set upsert() and you're ready to go.

For example:

$documentMannager->createQueryBuilder('App\Domain')
->findAndUpdate()
->returnNew()
->field('_id')->equals($id)
->field('page')->equals($page)
->field('count')->inc(1)
->upsert()
->getQuery()
->execute();

This is the way i implement a bucket pattern.

like image 36
Gui Avatar answered Oct 03 '22 21:10

Gui