Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine 2, undefined entity method findOneBy*

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

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

I have setup the mapping, the entity class was created using the console and I have updated the schema in the database. What could be causing this issue?

I'm using symfony2. Here is the line:

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

Any ideas?

like image 436
Damien Roche Avatar asked Mar 24 '11 21:03

Damien Roche


2 Answers

findOneById doesn't exist, try

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

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

You can check the Doctrine's class documentation here: EntityRepository

Edit: looks like findOneById does exist as long as the entity has a field "Id". Check the docs. Thx to Ryall for pointing it out

like image 123
Maragues Avatar answered Nov 09 '22 02:11

Maragues


What is the service definition of myproject.blog.repository.blog? 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 have one created on the fly by the EntityManager.

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

Or even shorter:

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

Taken from the Doctrine2 ORM Documentation.

like image 31
igorw Avatar answered Nov 09 '22 04:11

igorw