php - Optimize doctrine find and update code -
currently, way, how update selected row, through find & update.
private function turnonlight($entity_manager, $model, $year) { // optimized way update single column? // seems not need 2 rounds db communication trip. $car = $entity_manager->findoneby(array( 'model' => $model, 'year' => $year, 'light' => 0 )); if (is_null($car)) { return; } $car->light = 1; $entity_manager->persist($car); $entity_manager->flush(); } however, don't sense efficient enough, requires 2 db operations (find & update).
is there way optimize above code?
as people have mentioned in comments, correct.
if don't have reference object first thing retrieve this.
it may improve split 2 since if happends when retrieving it's easier debug or prepare this.
one little tip, when updating existing entity, existing mean in database, should not phone call call $entity_manager->persist($car).
the persist method lets doctrine know should maintain track of entity, because it's in database doctrine knows update automatically when of properties change.
to create long story short, calling flush enough.
php symfony2 doctrine2
No comments:
Post a Comment