【问题标题】:Doctrine Entity Manager accessDoctrine Entity Manager 访问
【发布时间】:2013-03-01 08:03:30
【问题描述】:

我按照本教程中描述的方式将我的 Codeigniter 与 Doctrine ORM 集成

http://www.joelverhagen.com/blog/2011/05/setting-up-codeigniter-2-with-doctrine-2-the-right-way/.

它说在 CodeIgniter 中访问控制器中的库的标准方式是 $this->libraryName->libraryMember->someMemberFunction;

这意味着我可以通过这种方式访问​​ Codeigniter Controller 中的 Doctrine Entity Manager $this->doctrine->em->someMemberFunction;

问题是我需要在其他地方访问实体管理器,而不仅仅是在控制器中。例如,我需要在一些扩展实体存储库的自定义 model_helper 类中使用实体管理器。我该如何使用它?

【问题讨论】:

    标签: php codeigniter doctrine entitymanager


    【解决方案1】:
    $ci = &get_instance();; //get instance of a codeigniter 'core'
    
    $ci->doctrine->em-> ... etc. to use accross the framework
    

    此外,Entity Repository 类扩展了EntityRepository 中的\Doctrine\ORM\EntityRepository.php

    这个类有受保护的变量(_em),它是EntityManager的一个实例;所以最终你的 repositoryClass,假设类别和列出类别的函数看起来像这样:

    class Categories extends EntityRepository {
    
    public function getCategoryList($parent_id = 0) {
    
     $dql = "SELECT c FROM Entities\Categories c WHERE c.parent_id=:parent_id ORDER BY c.category_name ASC";
    
        try {
            $query = $this->_em->createQuery($dql);
            $query->setParameter('parent_id', $parent_id);
    
            return $query->getResult();
        } catch (Exception $e) {
    
            echo $e->getMessage() . '< br />';
            return;
        }
    }
    

    大声笑,刚刚注意到这是很久以前发布的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-10
      • 1970-01-01
      • 2013-07-29
      • 1970-01-01
      • 2014-09-30
      • 2015-01-25
      • 2013-08-05
      • 1970-01-01
      相关资源
      最近更新 更多