Zend框架2 +主义2 [英] Zend Framework 2 + Doctrine 2

查看:94
本文介绍了Zend框架2 +主义2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开始与Zend框架开发,我想用ZF2。由于我使用Doctrine 2,您能否提供一些教程,以帮助我在ZF2它集成?谢谢!

解决方案

上次检查:ZF2.2。*,DoctrineORMModule 0.7。



官方模块



您可能需要通过作曲者加载 DoctrineORMModule




  • doctrine / doctrine-orm-module 添加到您的作曲家.json 的要求(列表bcs格式化问题后的示例代码)

  • 运行 php composer.phar install / li>
  • 创建目录 ./ data / DoctrineORMModule / Proxy 并确保您的应用程序的写入权限

  • 通过应用程序配置学说 / config / autoload 给模块项目特定的设置(数据库等)

  • 在你的模块中配置doctrine的实体映射 config.php

  • 向您的项目添加实体

  • 添加 DoctrineORMModule DoctrineModule 到您的 config / application.config.php

  • 运行cli工具来生成你的表 ./ vendor / bin / doctrine-module orm:schema-tool:create



我强烈不鼓励您不使用作曲家,因为它是一种简单的方法来安装依赖关系,并使自动装载机全部设置。另外ZF2默认情况下通过作曲家出货。



示例代码



composer.json



  {
require:{
php:> = 5.3.3,
zendframework / zendframework:2. *
doctrine / doctrine-orm-module:0. *
}
}
/ pre>

实体设置



 <?php 
return array(
'doctrine'=> array(
'driver'=> array(
//定义一个带有两个路径的注释驱动程序,并将其命名为`my_annotation_driver`
'my_annotation_driver'=>数组(
'class'=>'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache'=>'array',
'paths'=>数组(
'path / to / my / entities',
'another / path



//默认元数据驱动程序,汇集了所有其它驱动器集成到一个单一的一个。
//只有当你知道你在做什么时才覆盖`orm_default`
'orm_default'=>数组(
'drivers'=>数组(
//注册`my_annotation_driver`用于命名空间下的任何实体`My\Namespace`
'My\Namespace'=>'my_annotation_driver '




)的

需要注意的是:您的信息的路径应该是完全合格的。最好从 __ DIR __ 开始,否则事情会中断(每个新项目我不知道为什么命令行工具不起作用,直到我发现这个错误...;)



连接设置



 <?php 
return array(
'doctrine'=>数组(
'connection'=>数组(
//默认连接名称
'orm_default'=>数组(
'driverClass '="'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params'=>数组(
'host'=>'localhost',
'port'=>'3306',
'user'=>'用户名',
'密码'=>'密码',
'dbname'=>'数据库',



),
);

所有代码示例都是官方教义模块自述的一部分



进一步阅读:



Marco Pivetta做了一个关于教义模块的精彩演示使用,我建议大家使用这个模块。



Jason Grimes在phpdeveloper.org上发布了一个教程,该教程应该有助于在官方模块之前安装原则。


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!

解决方案

last time checked: ZF2.2.*, DoctrineORMModule 0.7.

Official Module

You may want to load DoctrineORMModule via composer:

  • 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 and DoctrineModule to your config/application.config.php
  • run the cli tool to generate your tables ./vendor/bin/doctrine-module orm:schema-tool:create

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.

Example Code

composer.json

{  
    "require" : {  
        "php": ">=5.3.3",  
        "zendframework/zendframework": "2.*"                
        "doctrine/doctrine-orm-module": "0.*"                
    }  
}  

entities settings

<?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'
                )
            )
        )
    )
);

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 ... ;)

connection settings

<?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

Further Reading:

Marco Pivetta made a wonderful presentation about doctrine-module usage, which I recommend to everybody using this module.

Jason Grimes wrote a tutorial featured on phpdeveloper.org which should help installing doctrine, before there was an official module.

这篇关于Zend框架2 +主义2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆