To save entity with doctrine I should do like this
$em = $this->getDoctrine()->getEntityManager();
$em->persist($product);
$em->flush();
But maybe I can somehow do that in one line like
$product->save();
or
$this->saveEntity($product);
or
$this->getDoctrineEntityManager()->persistAndFlush($product);
If I need to create this methods by my self then how to do that in symfony way?
Well
persist()andflush()are totally different and independent operation. When you persist an entity object you are telling the entity manager to track changes of the object. When you callflush()method the entity manager will push the changes of the entity objects the entity manager tracks to the database in single transaction. Most of the time entity manager have to manage multiple object. For example besides yourproductentity you may also have to tracktagorcartentity. CallingpersistAndFlush()every time when you save those entity object will cause multiple IO connection to DB. Which is not efficient. So I think it is better to treat them as a separate operation.