Doctrine 2, undefined entity method findOneBy*
I have a strange issue. Here is the error message:
Call to undefined m开发者_开发问答ethod 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?
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
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.
try this
$blogRepo = $this->getRepository('myproject.blog.repository.blog');
$blog = $blogRepo->findOneById($id);
getRepository
精彩评论