2014-12-31 45 views
1

我想实现一些服务,我做的第一件事是确定的私人$ EM为的EntityManager如下:捕致命错误:EntityManager的不能转换为字符串

<?php 

namespace Users\UsersBundle\Services; 

use Doctrine\ORM\EntityManager; 
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 
use Usuarios\UsersBundle\Entity\User; 

/** 
* Class UserManager 
*/ 
class UserManager 
{ 
private $em; 

/** 
* @param EntityManager $em 
*/ 
public function __construct(EntityManager $em) 
{ 
    $this->$em = $em; 
} 
} 

的例子

/** 
* Find all posts for a given author 
* 
* @param User $author 
* 
* @return array 
*/ 
public function findPosts(User $author) 
{ 
    $posts = $this->$em->getRepository('BlogBundle:Post')->findBy(array(
      'author' => $author 
     ) 
    ); 

    return $posts; 
} 

然而,当我打电话例如像一个我上面显示,我收到以下错误的任何功能:开捕致命错误:类学说的对象\ ORM \使用EntityManager非常相同的类中的功能EntityManager无法转换为字符串。

我确实导入了服务。我错过了什么?预先感谢您对我们的支持。

回答

1

取而代之的是:

$posts = $this->$em->getRepository('BlogBundle:Post')->findBy(array(
     'author' => $author 

试试这个:

$posts = $this->em->getRepository('BlogBundle:Post')->findBy(array(
     'author' => $author 

请注意,我从$这个改变 - > $ EM到这个 - $> EM。

1
$this->$em 

应该是:

$this->em 

$此 - > $ EM试图$ EM转换为字符串时,它是一个对象。除非对象定义了__toString()方法,否则您将得到一个异常。

相关问题