what is doctrine?

Post on 13-May-2015

7.394 Views

Category:

Technology

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

Beginner presentation introducing the Doctrine ORM and the basic operations. Also talks a bit about some upcoming versions 1.2 and 2.0.

TRANSCRIPT

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

What is Doctrine?

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Doctrine is a ORMwritten in PHP

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

What is an ORM?

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

ORM stands forObject Relational Mapper

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Manipulate RDBMSas a set of PHP objects

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Easy to get started

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Doctrine Sandbox

$ svn co http://svn.doctrine-project.org/branches/1.1 lamp_doctrine$ cd lamp_doctrine/tools/sandbox$ php doctrine.php

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Doctrine Sandbox

Command Line

Phonenumber: columns: phonenumber: type: string(55) notnull: true user_id: type: integer notnull: true relations: User: foreignAlias: Phonenumbers onDelete: CASCADE

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Test SchemaUser: columns: first_name: type: string(255) notnull: true last_name: type: string(255) notnull: true username: type: string(255) unique: true notnull: true password: type: string(255) notnull: true email_address: type: string(255) notnull: true email: true unique: true

lamp_doctrine/tools/sandbox/schema/schema.yml

User: jwage: first_name: Jonathan last_name: Wage username: jwage password: changeme email_address: jonathan.wage@sensio.com Phonenumbers: cell: phonenumber: 6155139185 office: phonenumber: 4159925468

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Test Datalamp_doctrine/tools/sandbox/data/fixtures/data.yml

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Build All Reload

$ php doctrine.php build-all-reload

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Running Tests• First DQL Query• Working with objects• Writing DQL Queries

– SELECT queries– UPDATE queries– DELETE queries

• Custom Accessors/Mutators• Using Migrations

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

First DQL Query$ php doctrine.php dql "FROM User u, u.Phonenumbers p"

$user = new User();$user->first_name = 'Fabien';$user->last_name = 'Potencier';$user->username = 'fabpot';$user->password = 'changeme';$user->email_address = 'fabien.potencier@sensio.com';$user->save();

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Working with Objects

Insert new User

$ php index.php

lamp_doctrine/tools/sandbox/index.php

$userTable = Doctrine::getTable('User');

$user = $userTable->findOneByUsername('jwage');$user->password = 'newpassword';$user->save();

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Working with Objects

Edit existing User

lamp_doctrine/tools/sandbox/index.php

$ php index.php

$userTable = Doctrine::getTable('User');

$user = $userTable->findOneByUsername('fabpot');

$user->Phonenumbers[]->phonenumber = '1233451234';$user->Phonenumbers[]->phonenumber = '9875674543';

$user->save();

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Working with Objects

Adding Phonenumbers

lamp_doctrine/tools/sandbox/index.php

$ php index.php

$userTable = Doctrine::getTable('User');

$user = $userTable->findOneByUsername('fabpot');$user->Phonenumbers[0]->delete();

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Working with Objects

Deleting Objects

lamp_doctrine/tools/sandbox/index.php

$ php index.php

$userTable = Doctrine::getTable('User');

$user = $userTable->findOneByUsername('fabpot');$user->Phonenumbers->delete();

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Working with Objects

Deleting Collections

lamp_doctrine/tools/sandbox/index.php

$ php index.php

$q = Doctrine::getTable('User') ->createQuery('u');

$users = $q->execute();

print_r($users->toArray());

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Writing DQL Queries

SELECT Queries

lamp_doctrine/tools/sandbox/index.php

$ php index.php

Array( [0] => Array ( [id] => 1 [first_name] => Jonathan [last_name] => Wage [username] => jwage [password] => changeme [email_address] => jonathan.wage@sensio.com )

[1] => Array ( [id] => 2 [first_name] => Fabien [last_name] => Potencier [username] => fabpot [password] => changeme [email_address] => fabien.potencier@sensio.com )

) What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Writing DQL Queries

$q = Doctrine::getTable('User') ->createQuery('u') ->leftJoin('u.Phonenumbers p') ->andWhere('u.username = ?', 'jwage');

$users = $q->execute();

print_r($users->toArray(true));

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Writing DQL Queries

SELECT Queries

lamp_doctrine/tools/sandbox/index.php

$ php index.php

Array( [0] => Array ( [id] => 1 [first_name] => Jonathan [last_name] => Wage [username] => jwage [password] => changeme [email_address] => jonathan.wage@sensio.com [Phonenumbers] => Array ( [0] => Array ( [id] => 1 [phonenumber] => 6155139185 [user_id] => 1 [User] => )

[1] => Array ( [id] => 2 [phonenumber] => 4159925468 [user_id] => 1 [User] => )

)

)

)

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Writing DQL Queries

$q = Doctrine::getTable('User') ->createQuery('u') ->update() ->set('email_address', '?', 'jonwage@gmail.com') ->andWhere('username = ?', 'jwage');

$affectedRows = $q->execute();

echo $affectedRows; // 1

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Writing DQL Queries

UPDATE Queries

lamp_doctrine/tools/sandbox/index.php

$ php index.php

$q = Doctrine::getTable('User') ->createQuery('u') ->delete() ->andWhere('username = ?', 'jwage');

$affectedRows = $q->execute();

echo $affectedRows; // 1

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Writing DQL Queries

DELETE Queries

lamp_doctrine/tools/sandbox/index.php

$ php index.php

// ...$manager = Doctrine_Manager::getInstance();$manager->setAttribute('auto_accessor_override', true);

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Custom AccessorsEnable Auto Accessor Override

lamp_doctrine/tools/sandbox/config.php

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Custom Accessors

Custom `name` accessor

class User extends BaseUser{ public function getName() { return trim($this->first_name.' '.$this->last_name); }}

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Custom Accessorslamp_doctrine/tools/sandbox/models/User.php

$q = Doctrine::getTable('User') ->createQuery('u') ->andWhere('username = ?', 'jwage');

$user = $q->fetchOne();

echo $user->name; // Jonathan Wage

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Custom Accessorslamp_doctrine/tools/sandbox/index.php

$ php index.php

class User extends BaseUser{ // ...

public function setPassword($password) { $this->_set('password', md5($password)); }}

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Custom Mutator

MD5 encrypt passwords

$q = Doctrine::getTable('User') ->createQuery('u') ->andWhere('username = ?', 'jwage');

$user = $q->fetchOne();

$user->password = 'changeme';$user->save();

echo $user->password; // 4cb9c8a8048fd02294477fcb1a41191a

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Custom Mutator

MD5 encrypt passwords

echo $user->name;echo $user->get('name');echo $user['name'];

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Accessor/Mutator SyntaxObject property, function, array access

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Database Migrations

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Deploy schema changes

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Maintain production data

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Programmatic interfacefor issuing DDL statements

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Versions your database

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Upgrade your database to new versionsDowngrade to previous versions

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Database Migrations

Add new status column to users

User: columns:# ... status: type: enum values: [Pending, Active, Inactive]

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Database Migrations

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Database Migrations

Generate migration class

$ php doctrine generate-migrations-diff

class Version1 extends Doctrine_Migration_Base{ public function up() { $this->addColumn('user', 'status', 'enum', '', array('values' => array(0 => 'Pending', 1 => 'Active', 2 => 'Inactive'))); }

public function down() { $this->removeColumn('user', 'status'); }}

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Database MigrationsGenerate migration class

class Version1 extends Doctrine_Migration_Base{ public function postUp() { Doctrine::loadModels(realpath(dirname(__FILE__).'/../models'));

Doctrine::getTable('User') ->createQuery('u') ->update() ->set('status', '?', 'Active') ->execute(); }

// ...}

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Database MigrationsCustomize migration class

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Database MigrationsExecuting migration

$ php doctrine migratemigrate - migrated successfully to version #1

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Database MigrationsUpdate models from YAML

$ php doctrine generate-models-yaml

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Database MigrationsInspect migration was

successful or not

New status column existsDefault `Active` value

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Future of Doctrine?

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

New Versions

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Doctrine 1.2

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Continued evolutionof the 1.x codebase

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Doctrine 2.0

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Almost complete re-rewrite

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Major version

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Requires PHP 5.3

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Performance increases from 5.3

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Test suite runs20% faster

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

And uses 30%less memory

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Hydration performanceimprovements

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Doctrine 1.14.3435637950897 for 5000 records

Doctrine 2.01.4314442552312 for 5000 records

Doctrine 2.03.4690098762512 for 10000 records

Hydration Performance

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Removed Major Limitation

class User extends Doctrine_Record{ public function setTableDefinition() { $this->hasColumn('id', 'integer', null, array( 'primary' => true, 'auto_increment' => true ));

$this->hasColumn('username', 'string', 255); }}

No need to extend a base class

/** * @DoctrineEntity * @DoctrineTable(name="user") */class User{ /** * @DoctrineId * @DoctrineColumn(type="integer") * @DoctrineGeneratedValue(strategy="auto") */ public $id;

/** * @DoctrineColumn(type="varchar", length=255) */ public $username;}

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

No more crazy cyclic references

User Object( [id] => [username] => jwage)

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

print_r() your objects

$user = new User();$user->username = 'jwage';print_r($user);

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Positive effects of removing the base

class all around

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

GeneralImprovements

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Code de-coupled

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

3 Main Packages

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Common

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

DBAL

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

ORM

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Use Doctrine DBALseparate from the ORM

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Easier to extend and override things

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Better support formultiple databases

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Sequences, schemasand catalogs

$config = new \Doctrine\ORM\Configuration();$eventManager = new \Doctrine\Common\EventManager();$connectionOptions = array( 'driver' => 'pdo_sqlite', 'path' => 'database.sqlite');$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config, $eventManager);

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Simplified connectioninformation

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

No more DSN nightmares

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Connection informationspecified as arrays

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Removed oldattribute system

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Replaced with simplerstring based system

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Real Native SQL support

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Driver BasedMeta Data

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

/** * @DoctrineEntity * @DoctrineTable(name="user") */class User{ /** * @DoctrineId * @DoctrineColumn(type="integer") * @DoctrineGeneratedValue(strategy="auto") */ public $id;

/** * @DoctrineColumn(type="varchar", length=255) */ public $username;}

PHP Annotations

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

PHP Codeclass User{ public $id, $username;}

$metadata = new ClassMetadata('User');

$metadata->mapField(array( 'fieldName' => 'id', 'type' => 'integer', 'id' => true));

$metadata->setIdGeneratorType('auto');

$metadata->mapField(array( 'fieldName' => 'username', 'type' => 'varchar', 'length' => 255));

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

YAML

User: properties: id: id: true type: integer idGenerator: auto username: type: varchar length: 255

class User{ public $id, $username;}

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Write your own driver

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Cache

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Query CacheCache final SQL that is parsed from DQL

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Metadata CacheCache the parsing of meta data

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Result CacheCache the results of your queries

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

InheritanceMapping

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Single TableOne table per hierarchy

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Class TableOne table per class

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Concrete TableOne table per concrete class

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Testing

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Switched to phpUnit

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Better mock testing

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Easy to run tests against multiple DBMS

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Code de-coupled soit is easier to test

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

New Features

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

New DQL Parser

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Hand written

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Recursive-descentparser

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Constructs AST

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

PHP Class namesdirectly represent

DQL language

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Every DQL featurehas a class to handle

parsing

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Easy to maintainEasy to add new features

Easy to use

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Performance?

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Final SQL can beeasily and effectively cached

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Not practical to parseevery time

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

CustomColumn Types

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Add your own data types

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Types are OOP classes

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Easy to extendor add new types

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Extend DQL

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

DQL parser canbe extended

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Add your ownDQL functions

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

When?

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

First releasein September 09`

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

ALPHA

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

BETA

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

RC

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Stable - 2010’ ?

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

What is next?

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Publishing of firstDoctrine book

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Write more documentation

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Publish more books

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Doctrine communityextension repository

Symfony has Plugins

and

Doctrine has Extensions

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Default DBALand ORM in PEAR2?

De-facto standard?

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

It is up to you! :)

You can contact Jonathan about Doctrine and Open-Source or for training, consulting, application development, or business related

questions at jonathan.wage@sensio.com

What is Doctrine? www.doctrine-project.org www.sensiolabs.com

Nashville LAMP

Jonathan H. Wagejonathan.wage@sensio.com+1 415 992 5468

sensiolabs.com | doctrine-project.org | sympalphp.org | jwage.com

Questions?

top related