Zend Framework + Doctrine - introduction

Some time ago i have shown you how to integrete Doctrine with our Zend Framework. Because it is really hard to say how much i hate the Zend_Db_Table ideology of working with database (i think it is not only my opinion), i will try to show you advantages of the Doctrine way. In this post I only want to give you some really easy introduction to Doctrine with Zend (it is just introduction without real world examples).

Doctrine

is an object relational mapper (ORM) for PHP 5.2.3 and later that sits on top of a powerful database abstraction layer (DBAL). One of its key features is the option to write database queries in a proprietary object oriented SQL dialect called Doctrine Query Language (DQL), inspired by Hibernates HQL. This provides developers with a powerful alternative to SQL that maintains flexibility without requiring unnecessary code duplication.

Configuration

I moved our doctrine configuration to config.ini in config directory:
type       = “mysql”     // database adapter (Doctrine supports all drivers which PDO supports)
dbname = “doctrine_test”  // database name
host       = “localhost”  // host
username = “root”
password = “root”

Available Tasks

Using doctrine-clip.php that we created in the last post about Zend Framework we can call some really useful tasks. To get a list of the doctrine tasks we need to call in command line interface: “php doctrine-cli.php” (remember: we need to be in the main application directory):

doctrine-cli.php build-all
doctrine-cli.php build-all-load
doctrine-cli.php build-all-reload
doctrine-cli.php compile
doctrine-cli.php create-db
doctrine-cli.php create-tables
doctrine-cli.php dql
doctrine-cli.php drop-db
doctrine-cli.php dump-data
doctrine-cli.php generate-migration
doctrine-cli.php generate-migrations-
doctrine-cli.php generate-migrations-
doctrine-cli.php generate-models-db
doctrine-cli.php generate-models-yaml
doctrine-cli.php generate-sql
doctrine-cli.php generate-yaml-db
doctrine-cli.php generate-yaml-models
doctrine-cli.php load-data
doctrine-cli.php migrate
doctrine-cli.php rebuild-db

We don’t need do know what are all that tasks for. In the next post I will try to explain some of them - the most useful.

Schema.yml

This is one one of the most important configuration file. We put here our database structure in the yml format, so we can generate sql, models for database (there is a way to generate model direct form the database but without relations).

Doctrine data types are standardized and made portable across all DBMS. For the types that the DBMS do not support natively, Doctrine has the ability to convert the data on the way in to the and on the way out of the database. For example the Doctrine array and object types are serialized() on the way in and unserialized() on the way out.

Type MySQL Type
integer integr
float double
decimal decimal
string varchar
array text
object text
blob longblob
clob longtext
timestamp datetime
time time
date date
enum varchar/enum
gzip text

We will see schema file in action in next post. Now i only want to show you that there is something like yml file and how it should looks.

Simple table definition:


News:
  tableName: news
  columns:
    id:
      primary: true
      autoincrement: true
      type: integer(10)
    title: string(255)
    content: clob
    updated_at: timestamp(25)
    created_at: timestamp(25)

Calling the “php doctrine-clip.php generate-sql” will generate the following code:


CREATE TABLE news (
  id BIGINT AUTO_INCREMENT,
  title VARCHAR(255),
  content LONGTEXT,
  updated_at DATETIME,
  created_at DATETIME,
  PRIMARY KEY(id)
) ENGINE = INNODB;

There is a lot of great Doctrine features that i will not write about here - you can simply check the Doctrine Documentation. Next time i will do my best to show you the most important and common things that could simplify our database and project managment. If you have any question or suggestion about what you would like to see in the next post - just write me a mail or leave a comment.

P. S. At the begining i wanted to write my blog in polish, but then i a got a message if i could translate my post into english. So, now i’m tyring (as you can see i’m not good in English) to write posts in English - if you are really a beginner and you want me to translate any post - write a mail.

Od początku planowałem pisać bloga po Polsku. Docelową grupą mieli być początkujący Polscy programiści PHP. Od momentu gdy dostałem prośbę o przetłumaczenie wpisu, staram się pisać po Angielsku ( staram się - myślę, że jest to najlepesze określenie na sposób w jaki piszę :> ). W razie problemów z jakże zaawansowanym językiem angielskim w którymś moich z postów - dajcie znać - chętnie to przetłumacze.

2 Responses to “Zend Framework + Doctrine - introduction”


  1. 1 jason kenny

    Nice writing. You are on my RSS reader now so I can read more from you down the road.

  2. 2 Kamil Adryjanek

    Thanks, I wanted to write more posts about Zend and Doctrine but lately i was really busy. Maybe next week week.
    Cheers, Kamil

  1. 1 Kamil Adryjanek

Leave a Reply

PodglÄ…d komentarza:




About me:

  • PHP programmer
  • Symfony developer
  • Zend framework developer

Categories: