1

I wanted to implement some services, and the first thing I did was to define the private $em as the EntityManager as follows:

<?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;
}
}

An example of a function using the EntityManager inside the very same class:

/**
 * 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;
}

However when I call any function like for example the one I show above, I get the following error: Catchable Fatal Error: Object of class Doctrine\ORM\EntityManager could not be converted to string.

I did import the service. What am I missing? Thank you in advance for your support.

2 Answers 2

3
$this->$em 

Should be:

$this->em

$this->$em is trying to convert $em to a string when it's an object. Unless an object has a __toString() method defined, you'll get an exception.

Sign up to request clarification or add additional context in comments.

Comments

1

Instead of this:

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

try this:

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

Notice that I changed from $this->$em to $this->em.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.