Installing and configuring ZfcUser module and Doctrine2
Next task is to install ZfcUser module which will deal with User management in our Blog application and Doctrine2 which will be used as ORM (Object Relational Mapper). This is very easy, we just need to add some lines in our composer.json file and run update command. So, open your composer.json file (in project root folder) and make it look like this (I added other dependencies like Doctrine2 and Zend Developer tools, so we have everything we need to work with):/composer.json
1
2
3
4
5
6
7
8
9
10
| { |
itadmin@itadmin-AcerPower-Series:/var/www/pod$ composer.phar update
|
itadmin@itadmin-AcerPower-Series:/var/www/pod$ composer.phar update
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing zf-commons/zfc-base (v0.1.2)
Loading from cache
- Installing zf-commons/zfc-user (dev-master fbbe5fd)
Cloning fbbe5fdeabd9229bf7d1328358c2d95538a6081e
- Installing doctrine/lexer (v1.0)
Loading from cache
- Installing doctrine/annotations (v1.1.2)
Loading from cache
- Installing doctrine/collections (v1.1)
Loading from cache
- Installing doctrine/cache (v1.1)
Loading from cache
- Installing doctrine/inflector (v1.0)
Loading from cache
- Installing doctrine/common (v2.4.1)
Loading from cache
- Installing doctrine/dbal (v2.4.0)
Loading from cache
- Installing symfony/console (v2.3.4)
Loading from cache
- Installing doctrine/orm (v2.4.0)
Loading from cache
- Installing doctrine/doctrine-module (0.7.2)
Loading from cache
- Installing doctrine/doctrine-orm-module (0.7.0)
Loading from cache
- Installing zf-commons/zfc-user-doctrine-orm (dev-master 08be7b2)
Cloning 08be7b2a5e1ee31a3907582261c113531d2bf964
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 doctrine/migrations (doctrine migrations if you want to keep your schema definitions versioned)
Writing lock file
Generating autoload files
Let me explain what just happened. Composer just installed all those modules into our project’s vendor folder. Well, for them to work with our application, we need to tell the application they exist, so it can use them properly.
Open /config/application.config.php and let us add those modules so we can use them:
/config/application.config.php
1
2
3
4
5
6
7
8
9
10
| ... 'modules' => array ( 'Application' , 'DoctrineModule' , 'DoctrineORMModule' , 'ZfcBase' , 'ZfcUser' , 'ZfcUserDoctrineORM' , ), ... |
First, create a new database for this and call it zf2blogtutorial (or whatever you want). After that, copy /config/autoload/local.php.dist to /config/autoload/local.php and add this to it:
/config/autoload/local.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| // ... 'doctrine' => array ( 'connection' => array ( 'orm_default' => array ( 'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver' , 'params' => array ( 'host' => 'localhost' , 'port' => '3306' , 'user' => '****' , 'password' => '****' , 'dbname' => 'zf2blogtutorial' , ) ) ) ), |
1
| SQLSTATE[42S02]: Base table or view not found: 1146 Table 'zf2blogtutorial.user' doesn't exist |
Doctrine ORM module for Zend Framework 2 comes with handy tools which will help us with creating our application from top down. What does this mean?
Well, you first create an Entity and then generate a database schema. We did not create an Entity for ZfcUser, as it already has one included in it’s Doctrine module. So, let’s create the schema.
So, in your command line enter this in project root:
1
| vendor/bin/doctrine-module orm:validate-schema |
1
2
3
| $ vendor/bin/doctrine-module orm:validate-schema [Mapping] OK - The mapping files are correct. [Database] FAIL - The database schema is not in sync with the current mapping file. |
1
| vendor/bin/doctrine-module orm:schema-tool:update --force |
It is working! Woot! Don’t worry about the looks of ZfcUser screens, that is because Skeleton application is using Twitter Bootstrap 3.0 and ZfcUser 2.*. We will fix that in next part.
Enabling ZendDeveloperTools
Let’s finish today’s session by enabling the handy Zend Developer tools. Open your /config/application.config.php and add Zend Developer Tools to the module list:/config/application.config.php
1
2
3
4
5
6
7
8
9
| 'modules' => array ( 'ZendDeveloperTools' , 'Application' , 'DoctrineModule' , 'DoctrineORMModule' , 'ZfcBase' , 'ZfcUser' , 'ZfcUserDoctrineORM' , ), |
If everything is ok, you should see the toolbar on bottom of your application if you refresh the browser.
No comments:
Post a Comment