php - Zend Framework 2 + Doctrine 2

标签 php zend-framework doctrine doctrine-orm zend-framework2

我想开始使用 Zend Framework 进行开发,我想使用 zf2。由于我使用 Doctrine 2,您能否建议一些教程来帮助我将其集成到 zf2 中?谢谢!

最佳答案

最后一次检查:ZF2.2.*,DoctrineORMModule 0.7。

官方模块

你可能想通过 composer 加载 DoctrineORMModule:

  • doctrine/doctrine-orm-module 添加到您的 composer.json 的 require(格式问题列表 bcs 之后的示例代码)
  • 运行 php composer.phar install
  • 创建目录 ./data/DoctrineORMModule/Proxy 并确保您的应用程序具有写入权限
  • configure doctrine通过您的应用程序 /config/autoload 为模块提供项目特定的设置(数据库等)
  • 在你的模块 config.php
  • 中配置 doctrine 的实体映射
  • 向您的项目添加一个实体
  • DoctrineORMModuleDoctrineModule 添加到您的 config/application.config.php
  • 运行 cli 工具生成你的表 ./vendor/bin/doctrine-module orm:schema-tool:create

我强烈建议你不要使用 composer,因为它是安装依赖项和设置自动加载器的简单方法。 ZF2 也默认通过 composer 发布。

示例代码

Composer .json

{  
    "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' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
                '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 `My\Namespace`
                    'My\Namespace' => 'my_annotation_driver'
                )
            )
        )
    )
);

需要注意的一个问题:实体的路径应该是完全限定的。最好从 __DIR__ 开始,否则事情会崩溃(每个新项目我都想知道为什么命令行工具在我发现这个错误之前不起作用......;)

连接设置

<?php
return array(
    'doctrine' => array(
        'connection' => array(
            // default connection name
            'orm_default' => array(
                'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
                'params' => array(
                    'host'     => 'localhost',
                    'port'     => '3306',
                    'user'     => 'username',
                    'password' => 'password',
                    'dbname'   => 'database',
                )
            )
        )
    ),
);

All code examples are part of the official doctrine module readme

进一步阅读:

Marco Pivetta 制作了一个 wonderful presentation about doctrine-module usage ,我向所有使用此模块的人推荐。

Jason Grimes wrote a tutorial在 phpdeveloper.org 上有特色,在有官方模块之前应该有助于安装 Doctrine 。

关于php - Zend Framework 2 + Doctrine 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7867198/

相关文章:

php - Doctrine 的多对多自引用和互惠

php - 使用 PHP 进行认证的随机数生成器

json - 如何使用序列化程序 (Symfony 4) 创建正确格式的 json 字符串?

php - 没有依赖注入(inject)的方法的模拟对象

php - 在 Zend Framework 中使用网站图标

php - 如何在 zend 框架中命名上传的文件

php - 在同一页面中使用多个表单时 Zend_Form 值会丢失

mysql - Doctrine 内连接

php - 根据查询结果在 html 表中生成复选框

php - 什么是用于创建缩略图的 Timthumb 的良好 PHP 替代品?