Doctrine 2, undefined method of findOneBy * method

I have a strange problem. Here is the error message:

Call the undefined method MyProject\BlogBundle\Entity\Blog::findOneById()

I set up the mapping, the entity class was created using the console, and I updated the schema in the database. What could be causing this problem?

I am using symfony2. Here is the line:

 $blogRepo = $this->get('myproject.blog.repository.blog'); $blog = $blogRepo->findOneById($id); 

Any ideas?

+6
symfony doctrine2
source share
3 answers

findOneById does not exist, try

$ blogRepo-> findOneBy (array ('id' => $ id));

where 'id' is an existing field in your Entity.

You can check the documentation of the Doctrine class here: EntityRepository

Edit: It looks like findOneById exists if the object has an "Id" field. Check documents . thanks to Ryall for pointing it out

+8
source share

What is the definition of the myproject.blog.repository.blog service? It looks like you are mapping it to MyProject\BlogBundle\Entity\Blog , while it really should be MyProject\BlogBundle\Entity\BlogRepository .

Instead of creating your own repository class, you can also create one of them created by EntityManager on the fly.

 $user = $em->getRepository('MyProject\Domain\User')->find($id); 

Or even shorter:

 $user = $em->find('MyProject\Domain\User', $id); 

Adapted from the Documentation of the Doctrine2 ORM Document .

+5
source share

try it

 $blogRepo = $this->getRepository('myproject.blog.repository.blog'); $blog = $blogRepo->findOneById($id); 

getRepository

0
source share

All Articles