Reputation: 2007
I have a generic PHP class I'm using to contain functions/methods I use in multiple Symfony Controllers. In order to use the entity manager, I'm passing it like so:
class MyController extends Controller
{
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$dbf = new DBFunctions($em);
$result = $dbf->doSomething();
}
}
My generic class looks like this:
class DBFunctions
{
private $em;
public function __construct($em1)
{
$em = $em1;
var_dump($em1);
}
public function doSomething(){
$query = $em->createQuery(<my query>);
return $query->getResult();
}
}
The problem is when I run the function I get the following error:
Call to a member function createQuery() on a non-object
BUT my var_dump shows this (and a ton more):
object(EntityManager514e73398155c_546a8d27f194334ee012bfe64f629947b07e4919\__CG__\Doctrine\ORM\EntityManager)[236]
private 'delegate' =>
object(Doctrine\ORM\EntityManager)[231]
I'm sure its an object, so I'm lost as to what to do.
Thanks for any help!
Upvotes: 3
Views: 1742
Reputation: 24413
The problem is that you can't have one variable pass from one method to another like that. If you do var_dump($em)
in your doSomething()
method, you will see that it returns NULL
. Either pass it as a parameter to that method or set $this->em
in your constructor.
Upvotes: 1