Symfony / Doctrine – update a record using models

The Doctrine manual is really, really confusing in places. If you want to do something as simple as updating a record, the examples suggest that you use Doctrine_Query::create(). This doesn’t make a lot of sense, because we only want to manipulate the model, we shouldn’t have to even look at a query. Assuming you have the primary id of the record in question, you can do the following to easily modify a record:


$myobj = new Doctrine::getTable('MyObj')->find($request->getParameter('my_id'));
$myobj->property = 'New Value';
$myobj->save();

This is really straightforward, but if you don’t include the find() inline with the getTable() call, you’ll get an obscure “Unknown method saveTableProxy” error, which there seems to be no documentation for anywhere on the internet. It only took a couple of hours of trial and error to get this figured out.

In general, I’d still recommend Doctrine as an ORM, but the lack of consistent and clear documentation, with dreadfully obscure object methods, make for a very steep learning curve.