Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Organize the models in Symfony 3

I just read the official Symfony 3 docs and point when I need to retrieve objects from database I should use something like this:

$repository = $em->getRepository('AppBundle:Product');

Here Product is just an entity class with no parent, so Doctrine work with it through annotations. But I'm not sure it's a good idea to hardcode the model name in quotes. What if later I conclude to name the model Good, should I search through the whole project and replace Product on Good. I Laravel for example, each model extends the base model class so I could write : Product::model()->find('nevermind') . Is there any such option in Symfony 3.3?

like image 876
James May Avatar asked Jun 26 '17 19:06

James May


2 Answers

I'm not shure if it is a solution for your problem, but you can write:

$repository = $em->getRepository(Product::class);
like image 67
Lukas Avatar answered Oct 20 '22 15:10

Lukas


The $em->getRepository('...') returns a mixed datatype (depends on the first parameter). Even you write $repository = $em->getRepository(Product::class); the IDE cannot resolve the real datatype. I suggest this method (pseudo code):

/**
 * Get product repo
 *
 * @return ProductRepository
 */
 public function getProductRepository() 
 {
     return $this->getEntityManager()->getRepository(Product::class);
 }

 /**
  * @return \Doctrine\ORM\EntityManager
  */
 public function getEntityManager(): EntityManager
 {
     return $this->getDoctrine()->getManager();
 }
like image 29
odan Avatar answered Oct 20 '22 16:10

odan