doumi4676 2013-03-01 08:03
浏览 72

Doctrine Entity Manager访问权限

I integrated my Codeigniter with Doctrine ORM the way described in this great tutorial

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

It says that standard way in CodeIgniter to access a library in a controller is $this->libraryName->libraryMember->someMemberFunction;

It means that i can access Doctrine Entity Manager in Codeigniter Controller this way $this->doctrine->em->someMemberFunction;

The problem is i need to access entity manager in some other place, not just in controller. E.g, i need to use entity manager in some custom model_helper class extending Entity Repository. How can i use it?

  • 写回答

1条回答 默认 最新

  • doulang6695 2013-05-30 15:35
    关注
    $ci = &get_instance();; //get instance of a codeigniter 'core'
    
    $ci->doctrine->em-> ... etc. to use accross the framework
    

    Also, Entity Repository classes extends EntityRepository class in \Doctrine\ORM\EntityRepository.php

    This class has protected variable (_em) which is an instance of EntityManager; So eventually your repositoryClass, lets say categories and function that lists categories would look something like this:

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

    lol, just noticed this was posted ages ago.

    评论

报告相同问题?

悬赏问题

  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件