doctrine集成到zend framework 2

来源:互联网 发布:都匀浪人网络 编辑:程序博客网 时间:2024/06/07 06:01

进入zf2 项目的根目录:/var/web/newzf2。前提是已经正确安装了zf2框架。

1. 执行如下命令:php composer.phar require doctrine/doctrine-orm-module:0.7.*

执行结果如下:

[/var/web/newzf2]# php composer.phar require doctrine/doctrine-orm-module:0.7.*./composer.json has been updatedLoading composer repositories with package informationUpdating dependencies (including require-dev)  - Installing doctrine/lexer (v1.0)    Downloading: 100%           - Installing doctrine/annotations (v1.1.2)    Downloading: 100%           - Installing doctrine/collections (v1.1)    Downloading: 100%           - Installing doctrine/cache (v1.3.0)    Downloading: 100%           - Installing doctrine/inflector (v1.0)    Downloading: 100%           - Installing doctrine/common (v2.4.1)    Downloading: 100%           - Installing doctrine/dbal (v2.4.1)    Downloading: 100%           - Installing symfony/console (v2.4.0)    Downloading: 100%           - Installing doctrine/orm (v2.4.1)    Downloading: 100%           - Installing doctrine/doctrine-module (0.7.2)    Downloading: 100%           - Installing doctrine/doctrine-orm-module (0.7.0)    Downloading: 100%         symfony/console suggests installing symfony/event-dispatcher ()doctrine/orm suggests installing symfony/yaml (If you want to use YAML Metadata Mapping Driver)doctrine/doctrine-module suggests installing doctrine/data-fixtures (Data Fixtures if you want to generate test data or bootstrap data for your deployments)doctrine/doctrine-orm-module suggests installing zendframework/zend-developer-tools (zend-developer-tools if you want to profile operations executed by the ORM during development)doctrine/doctrine-orm-module suggests installing doctrine/migrations (doctrine migrations if you want to keep your schema definitions versioned)Writing lock fileGenerating autoload files
2. 继续执行以下语句:

[/var/web/newzf2]# php composer.phar require zendframework/zend-developer-tools:dev-master
执行结果如下:

[root@VM49 newzf2]# php composer.phar require zendframework/zend-developer-tools:dev-master./composer.json has been updatedLoading composer repositories with package informationUpdating dependencies (including require-dev)  - Installing zendframework/zend-developer-tools (dev-master 8c38e67)    Cloning 8c38e67884268c94426cc4c8647d064ef13d100azendframework/zend-developer-tools suggests installing bjyoungblood/bjy-profiler (Version: dev-master, allows the usage of the (Zend) Db collector.)zendframework/zend-developer-tools suggests installing ocramius/ocra-service-manager (OcraServiceManager can help you track dependencies within your application.)Writing lock fileGenerating autoload files

3. 在根目录下执行:cp vendor/zendframework/zend-developer-tools/config/zenddevelopertools.local.php.dist zenddevelopertools.local.php
4. 配置新添加的模块

  配置文件:config/application.config.php

修改为:

return array(    'modules' => array(        'Application',        'User',        'Register',        'ZendDeveloperTools',        'DoctrineModule',        'DoctrineORMModule',        ),   ........    );
5. 写第一个doctrine entity

cd module/Application/src/Application

mkdir  Entity

cd Entity

创建User.php文件,格式如此,后面会根据这个entity自动生成表

namespace Application\Entity;use Doctrine\ORM\Mapping as ORM;/** @ORM\Entity */class User {    /**    * @ORM\Id    * @ORM\GeneratedValue(strategy="AUTO")    * @ORM\Column(type="integer")    */    protected $id;    /** @ORM\Column(type="string") */    protected $fullName;    // getters/setters   public function getId(){      return $this->id;    }    public function setId($id){      $this->id = $id;    }  public function getFullName(){      return $this->fullName;    }    public function setFullName($name){      $this->fullName = $name;    }}
6.配置文件添加mapping项cd  /var/web/newzf2/module/Application/config

修改module.config.php

在array中添加一个配置项:

'doctrine'=> array(        'driver'=>array(                'application_entities'=> array(                     'class'=>'Doctrine\ORM\Mapping\Driver\AnnotationDriver',                     'cache'=>'array',                     'paths'=>array(_DIR_.'/../src/Application/Entity')                ),                'orm_default'=>array(                     'drivers'=> array(                        'Application\Entity'=>'application_entities'                     )                )        )    ),

7. 配置数据库连接参数

创建:/var/web/newzf2/config/autoload/doctrine.local.php

内容如下:

retrun array('doctrine'=>array('connection'=>array('orm_default'=>array('driverClass'=>'Doctrine\DBAL\Driver\PDOMysql\Driver','params'=>array('host'=>'localhost','port'=>'3306','user'=>'username','password'=>'password','dbname'=>'database',)))));)

执行./vendor/bin/doctrine-module orm:validate-schema。

报错:

[/var/web/newzf2]$ ./vendor/bin/doctrine-module orm:validate-schemaPHP Warning:  class_implements(): Class Doctrine\DBAL\Driver\PDOMysql\Driver does not exist and could not be loaded in /var/web/newzf2/vendor/doctrine/dbal/lib/Doctrine/DBAL/DriverManager.php on line 183Warning: class_implements(): Class Doctrine\DBAL\Driver\PDOMysql\Driver does not exist and could not be loaded in /var/web/newzf2/vendor/doctrine/dbal/lib/Doctrine/DBAL/DriverManager.php on line 183PHP Warning:  in_array() expects parameter 2 to be array, boolean given in /var/web/newzf2/vendor/doctrine/dbal/lib/Doctrine/DBAL/DriverManager.php on line 183Warning: in_array() expects parameter 2 to be array, boolean given in /var/web/newzf2/vendor/doctrine/dbal/lib/Doctrine/DBAL/DriverManager.php on line 183PHP Fatal error:  Uncaught exception 'Doctrine\DBAL\DBALException' with message 'The given 'driverClass' Doctrine\DBAL\Driver\PDOMysql\Driver has to implement the \Doctrine\DBAL\Driver interface.' in /var/web/newzf2/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php:134Stack trace:#0 /var/web/newzf2/vendor/doctrine/dbal/lib/Doctrine/DBAL/DriverManager.php(184): Doctrine\DBAL\DBALException::invalidDriverClass('Doctrine\DBAL\D...')#1 /var/web/newzf2/vendor/doctrine/dbal/lib/Doctrine/DBAL/DriverManager.php(136): Doctrine\DBAL\DriverManager::_checkParams(Array)#2 /var/web/newzf2/vendor/doctrine/doctrine-orm-module/src/DoctrineORMModule/Service/DBALConnectionFactory.php(59): Doctrine\DBAL\DriverManager::getConnection(Array, Object(Doctrine\ORM\Configuration), Object(Doctrine\Common\EventManager))#3 [internal function]: DoctrineORMModule\Service\DBALConnectionFactory->createService(Object(Zend\ServiceManager\ServiceManager), 'doctrine.connec...', 'doctrine.connec...')#4 /var/web/newzf2/vendor/zendframework/zendframework/ in /var/web/newzf2/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php on line 904Fatal error: Uncaught exception 'Doctrine\DBAL\DBALException' with message 'The given 'driverClass' Doctrine\DBAL\Driver\PDOMysql\Driver has to implement the \Doctrine\DBAL\Driver interface.' in /var/web/newzf2/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php:134Stack trace:#0 /var/web/newzf2/vendor/doctrine/dbal/lib/Doctrine/DBAL/DriverManager.php(184): Doctrine\DBAL\DBALException::invalidDriverClass('Doctrine\DBAL\D...')#1 /var/web/newzf2/vendor/doctrine/dbal/lib/Doctrine/DBAL/DriverManager.php(136): Doctrine\DBAL\DriverManager::_checkParams(Array)#2 /var/web/newzf2/vendor/doctrine/doctrine-orm-module/src/DoctrineORMModule/Service/DBALConnectionFactory.php(59): Doctrine\DBAL\DriverManager::getConnection(Array, Object(Doctrine\ORM\Configuration), Object(Doctrine\Common\EventManager))#3 [internal function]: DoctrineORMModule\Service\DBALConnectionFactory->createService(Object(Zend\ServiceManager\ServiceManager), 'doctrine.connec...', 'doctrine.connec...')#4 /var/web/newzf2/vendor/zendframework/zendframework/ in /var/web/newzf2/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php on line 904

仔细看了一下,config/autoload/doctrine.local.php的driverClass是Doctrine\DBAL\Driver\PDOMySql\Driver而不是之前的Doctrine\DBAL\Driver\PDOMysql\Driver。

拼写错误而已。

所以配置文件config/autoload/doctrine.local.php为:

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

再执行一遍执行:

[/var/web/newzf2]# ./vendor/bin/doctrine-module orm:validate-schema[Mapping]  OK - The mapping files are correct.                                                                                [PDOException]                                                                                SQLSTATE[28000] [1045] Access denied for user 'username'@'localhost' (using password: YES)                                                                                                orm:validate-schema
提示mysql的账号无法访问,修改。

再次运行会遇到,首先要保证你的User.php要是之前格式化的文件


执行创建数据库操作:

./vendor/bin/doctrine-module orm:schema-tool:create

提示:


测试,doctrine。

编辑文件:module/Application/src/Application/Controller/IndexController.php

public function indexAction()    {        $objectManager = $this        ->getServiceLocator()        ->get('Doctrine\ORM\EntityManager');        $user = new \Application\Entity\User();        $user->setFullName('Marco Pivetta');        $objectManager->persist($user);        $objectManager->flush();        die(var_dump($user->getId()));    }

执行结果,结果。一切顺利

0 0
原创粉丝点击