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 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line