dousigan0499 2012-06-02 10:13
浏览 95
已采纳

Zend Framework 1.11包含Doctrine 2和命名空间

I've been using Doctrine in my project without explicitly namespacing any of my classes. This led to some problems with trying to organise my code into separate sub directories (or at least it seemed to). As such I've tried to implement namespaces in my code but I'm struggling and having tried the numerous solutions on here to no avail, I need to ask.

I've got the standard project structure:

application/
--models/
--services/
--controllers/
..etc

In my Bootstrap I've got the following (without namespaces in my code which works fine):

/**
* Initialize Doctrine
* @return Doctrine_Manager
*/
public function _initDoctrine() {
    // include and register Doctrine's class loader
    require_once('doctrine/Doctrine/Common/ClassLoader.php');

    $autoloader = \Zend_Loader_Autoloader::getInstance();

    require_once('doctrine/Doctrine/Common/ClassLoader.php');
    $commonLoader = new \Doctrine\Common\ClassLoader('Doctrine\Common', 'doctrine');
    $autoloader->pushAutoloader(array($commonLoader, 'loadClass'), 'Doctrine\Common');

    $dbalLoader = new \Doctrine\Common\ClassLoader('Doctrine\DBAL', 'doctrine');
    $autoloader->pushAutoloader(array($dbalLoader, 'loadClass'), 'Doctrine\DBAL');

    $ormLoader = new \Doctrine\Common\ClassLoader('Doctrine\ORM', 'doctrine');
    $autoloader->pushAutoloader(array($ormLoader, 'loadClass'), 'Doctrine\ORM');

    $modelLoader = new \Doctrine\Common\ClassLoader(NULL, APPLICATION_PATH . "/models");
    $autoloader->pushAutoloader(array($modelLoader, 'loadClass'), '');

    // create the Doctrine configuration
    $config = new \Doctrine\ORM\Configuration();

    // setting the cache ( to ArrayCache. Take a look at
    // the Doctrine manual for different options ! )
    $cache = new \Doctrine\Common\Cache\ArrayCache;
    $config->setMetadataCacheImpl($cache);
    $config->setQueryCacheImpl($cache);

    // choosing the driver for our database schema
    // we'll use annotations
    $driver = $config->newDefaultAnnotationDriver(
        APPLICATION_PATH . '/models'
        );
    $config->setMetadataDriverImpl($driver);

    // set the proxy dir and set some options
    $config->setProxyDir(APPLICATION_PATH . '/models/Proxies');
    $config->setAutoGenerateProxyClasses(true);
    //$config->setAutoGenerateProxyClasses(false);
    $config->setProxyNamespace('App\Proxies');

    // now create the entity manager and use the connection
    // settings we defined in our application.ini
    $connectionSettings = $this->getOption('doctrine');
    $conn = array(
    'driver'    => $connectionSettings['conn']['driv'],
    'user'      => $connectionSettings['conn']['user'],
    'password'  => $connectionSettings['conn']['pass'],
    'dbname'    => $connectionSettings['conn']['dbname'],
    'host'      => $connectionSettings['conn']['host']
    );
    $entityManager = \Doctrine\ORM\EntityManager::create($conn, $config);

    // push the entity manager into our registry for later use
    $registry = Zend_Registry::getInstance();
    $registry->entitymanager = $entityManager;

    return $entityManager;
}

When I add

namespace models;

to each of my model classes and update the Bootstrap to be as follows I get an Exception "Class Application does not exist" (Application is one of my models):

$modelLoader = new \Doctrine\Common\ClassLoader('models', APPLICATION_PATH . "/models");
$autoloader->pushAutoloader(array($modelLoader, 'loadClass'), 'models');

Just for completeness, I reference that model in my controller as follows:

public function indexAction()
{
    $this->_helper->layout()->title = "Your applications";
    $this->_helper->layout()->description = "Create, edit and view all applications you have registered with us.";
    $this->view->applicationList = $this->entityManager->getRepository("Application")->findAll();
}

What am I missing? I'm sure it's obvious but really am pulling out my hair now.

  • 写回答

1条回答 默认 最新

  • duanguane1670 2012-06-14 21:08
    关注

    After much futher searching, I stumbled across this video (login required).

    Basically, the way I solved (as per the webinar) was to namespace my entities and save them under the /library directory. Then whenever I need to use them from the actual app, I access via their namespace.

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

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥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