3

クラスの名前空間を明示的に指定せずに、プロジェクトで Doctrine を使用してきました。これにより、コードを別々のサブディレクトリに整理しようとすると、いくつかの問題が発生しました (または、少なくともそのように見えました)。そのため、コードに名前空間を実装しようとしましたが、苦労しており、ここで多くの解決策を試してみましたが役に立たなかったので、尋ねる必要があります。

私は標準的なプロジェクト構造を持っています:

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

私のBootstrapには次のものがあります(コードに名前空間がなくても問題なく動作します):

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

追加すると

namespace models;

各モデル クラスに追加し、Bootstrap を次のように更新すると、「クラス アプリケーションが存在しません」という例外が発生します (アプリケーションはモデルの 1 つです)。

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

完全を期すために、コントローラーでそのモデルを次のように参照します。

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

私は何が欠けていますか?当たり前のことだと思いますが、本当に今、髪を抜いています。

4

1 に答える 1

1

さらに検索した結果、このビデオに出くわしました(ログインが必要です)。

基本的に、私が (ウェビナーに従って) 解決した方法は、エンティティに名前を付けて、/library ディレクトリに保存することでした。次に、実際のアプリからそれらを使用する必要があるときはいつでも、それらの名前空間を介してアクセスします。

于 2012-06-14T21:08:27.630 に答える