問題描述
我想開始使用 Zend Framework 進行開發,并且我想使用 zf2.由于我使用 Doctrine 2,您能否建議一些教程來幫助我將其集成到 zf2 中?謝謝!
I would like to start developing with Zend Framework and I would like to use zf2. Since I use Doctrine 2, can you suggest some tutorials to help me to integrate it in zf2? Thanks!
推薦答案
上次檢查:ZF2.2.*, DoctrineORMModule 0.7.
您可能希望通過 composer 加載 DoctrineORMModule
:
You may want to load DoctrineORMModule
via composer:
- 將
doctrine/doctrine-orm-module
添加到您的 composer.json 的 require(格式問題列表后的示例代碼) - 運行
php composer.phar install
- 創建目錄
./data/DoctrineORMModule/Proxy
并確保您的應用程序具有寫入權限 - 配置原則通過你的應用程序
/config/autoload
給模塊項目特定的設置(數據庫等) - 在你的模塊中配置學說的實體映射
config.php
- 向您的項目添加實體
- 將
DoctrineORMModule
和DoctrineModule
添加到您的config/application.config.php
- 運行 cli 工具來生成你的表
./vendor/bin/doctrine-module orm:schema-tool:create
- add
doctrine/doctrine-orm-module
to your composer.json's require (example code after list bcs of formatting problems) - run
php composer.phar install
- create the directory
./data/DoctrineORMModule/Proxy
and ensure write access for your application - configure doctrine via your applications
/config/autoload
to give the module the project-specific settings (database etc) - configure doctrine's entity mapping in your modules
config.php
- add an entity to your project
- add
DoctrineORMModule
andDoctrineModule
to yourconfig/application.config.php
- run the cli tool to generate your tables
./vendor/bin/doctrine-module orm:schema-tool:create
我強烈建議您不要使用 Composer,因為這是安裝依賴項和設置自動加載器的簡單方法.默認情況下,ZF2 也通過 Composer 發布.
I strongly discourage you from not using composer, as it is an easy way to install dependencies and have the autoloaders all set up. Also ZF2 ships via composer by default.
{
"require" : {
"php": ">=5.3.3",
"zendframework/zendframework": "2.*"
"doctrine/doctrine-orm-module": "0.*"
}
}
實體設置
<?php
return array(
'doctrine' => array(
'driver' => array(
// defines an annotation driver with two paths, and names it `my_annotation_driver`
'my_annotation_driver' => array(
'class' => 'DoctrineORMMappingDriverAnnotationDriver',
'cache' => 'array',
'paths' => array(
'path/to/my/entities',
'another/path'
),
),
// default metadata driver, aggregates all other drivers into a single one.
// Override `orm_default` only if you know what you're doing
'orm_default' => array(
'drivers' => array(
// register `my_annotation_driver` for any entity under namespace `MyNamespace`
'MyNamespace' => 'my_annotation_driver'
)
)
)
)
);
需要注意的問題:通往實體的路徑應該是完全合格的.最好從 __DIR__
開始,否則事情會崩潰(每個新項目我都想知道為什么命令行工具在我發現這個錯誤之前不起作用......;)
A gotcha to be aware of: The paths to your entites should be fully qualified. Best start with __DIR__
, else things will break (Every new project I wonder why the command line tool doesn't work until I find this error ... ;)
<?php
return array(
'doctrine' => array(
'connection' => array(
// default connection name
'orm_default' => array(
'driverClass' => 'DoctrineDBALDriverPDOMySqlDriver',
'params' => array(
'host' => 'localhost',
'port' => '3306',
'user' => 'username',
'password' => 'password',
'dbname' => 'database',
)
)
)
),
);
所有代碼示例都是官方學說模塊自述文件的一部分
Marco Pivetta 做了一個關于學說模塊使用的精彩演講,我向所有使用此模塊的人推薦.
Marco Pivetta made a wonderful presentation about doctrine-module usage, which I recommend to everybody using this module.
Jason Grimes 寫了一個教程 在 phpdeveloper.org 上有特色,這應該有助于安裝學說,然后才有官方模塊.
Jason Grimes wrote a tutorial featured on phpdeveloper.org which should help installing doctrine, before there was an official module.
這篇關于Zend 框架 2 + Doctrine 2的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!