douou1872 2013-03-24 04:02
浏览 26
已采纳

将doctrine实体管理器作为参数传递给泛型类

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!

  • 写回答

1条回答 默认 最新

  • dsy48837 2013-03-24 04:07
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题