doctrine project

49
PHPMGTalks #3 – 13 de Abril de 2013

Upload: daniel-lima

Post on 07-Dec-2014

154 views

Category:

Software


8 download

DESCRIPTION

Ao contrário do que todo mundo pensa, o Doctrine não é somente um Mapeador de objeto relacional. É um projeto focado em desenvolver soluções para persistência de dados e tecnologias relacionadas. Nessa palestra você verá o uso de várias ferramentas que fazem o uso de pacotes do projeto que serão úteis no seu ambiente desenvolvimento desde a implementação ao deploy.

TRANSCRIPT

Page 1: Doctrine Project

PHPMGTalks #3 – 13 de Abril de 2013

Page 2: Doctrine Project

Eu!

● Daniel Lima– @yourwebmaker– fb.com/dlpinheiro– Arquiteto de aplicações na – Produtor musical & DJ– Biker– Skater– Drunk– ...

Page 3: Doctrine Project

Agenda

● O que é o Doctrine na verdade?– O projeto– Libs:

● ORM● ODM: MongoDB, CouchDB● DBAL: Migrations● Common: Annotations, Cache, Data Fixtures, Inflector

● Doctrine e outros projetos

Page 4: Doctrine Project

Vamos começar!

Page 5: Doctrine Project

O Projeto

● O que é o Doctrine na verdade?– “The Doctrine Project is the home of a selected set

of PHP libraries primarily focused on providing persistence services and related functionality. Its prize projects are a Object Relational Mapper and the Database Abstraction Layer it is built on top of.” - http://www.doctrine-project.org/

Page 6: Doctrine Project

Ou seja...

Page 7: Doctrine Project

Não é somente um ORM!

Page 8: Doctrine Project

Colaboradores

● Guilherme Blanco …. Éééééé do Brasilll!!!!● Jonathan H. Wage● Benjamin Eberlei● Bulat Shakirzyanov● Fabien Potencier● Roman S. Borschel● ….... Você!

Page 9: Doctrine Project

Agora, vamos conhecer algumas libraries do

Doctrine!

Page 10: Doctrine Project

ORM: Mapeador de Objeto Relacional● É a lib mais conhecida pela comunidade● Facilita o desenvolvimento voltado ao Domínio

(problema) da aplicação● Aproxima o modelo entidade-relacionamento

ao modelo Orientado a Objetos● Não é necessário escrever SQL● Não é esse MONSTRO todo que pensam...● RTFM!!!

Page 11: Doctrine Project

ORM: Mapeador de Objeto Relacional

Passo a passo comum:– Crie suas tabelas– Crie suas classes– Mapeie suas classes– Instancie suas classes– Salve seus objetos– Traga seus objetos salvos

Page 12: Doctrine Project

Crie suas tabelas no banco de dados

Page 13: Doctrine Project

Crie suas classes

Page 14: Doctrine Project

Crie suas classes

Page 15: Doctrine Project

Mapeie suas classes● Classe “Usuario” mapeada

– https://gist.github.com/yourwebmaker/5369280#file-usuario-php

● Classe “Grupo” mapeada– https://gist.github.com/yourwebmaker/5369280#fil

e-grupo-php

Page 16: Doctrine Project

Instancie suas classes

Page 17: Doctrine Project

Instancie suas classes

Salve seus objetos

Page 18: Doctrine Project

Traga seus objetos salvos

Page 19: Doctrine Project

ODM: Mapeador para No-SQL● Mesmo “Conceito” do ORM, mas trabalha

somente com bancos No-Sql● Oferece funcionalidades extras que os Clients

No-Sql não oferecem:– Event Manager– EntityRepositories– Herança

● MongoDB, CouchDB, OrientDB...● O projeto Doctrine\KeyValueStore pretende

padronizar o uso de bancos No-Sql.

Page 20: Doctrine Project

DBAL: Data Base Abstraction & Access Layer

● Camada de abstração de banco de dados sobre a PDO

● Oferece funcionalidades extras à PDO● ORM faz uso da DBAL

Page 21: Doctrine Project

DBAL: Alguns Recursos extras à PDO● Cache

Page 22: Doctrine Project

DBAL: Alguns Recursos extras à PDO● Eventos

Page 23: Doctrine Project

DBAL: Alguns Recursos extras à PDO● Tipos

– CREATE DOMAIN MyMoney AS DECIMAL(18,3);– Definindo um tipo:

https://gist.github.com/yourwebmaker/7a61f175c759cf00096d#file-moneytype-php

– Adicionando um tipo:

Page 24: Doctrine Project

DBAL: Alguns Recursos extras à PDO● Schema-Manager

Page 25: Doctrine Project

DBAL: Alguns Recursos extras à PDO● Schema-Representation

Page 26: Doctrine Project

DBAL: Alguns Recursos extras à PDO● Schema-Representation

Page 27: Doctrine Project

DBAL: Alguns Recursos extras à PDO● Query Builder

Page 28: Doctrine Project

Common\Annotations● /**

* @var * @todo * @Entity(repositoryClass=”XRepository”) * @ORM\Entity * @Assert\NotBlank() */

● "Annotations provide data about a program that is not part of the program itself. They have no direct effect on the operation of the code they annotate." Oracle

● São como arquivos de configuração

Page 29: Doctrine Project

Common\Annotations

Page 30: Doctrine Project

Common\Cache● ApcCache (requires ext/apc)● ArrayCache (in memory, lifetime of the request)● FilesystemCache (not optimal for high concurrency)● MemcacheCache (requires ext/memcache)● MemcachedCache (requires ext/memcached)● PhpFileCache (not optimal for high concurrency)● RedisCache.php (requires ext/phpredis)● WinCacheCache.php (requires ext/wincache)● XcacheCache.php (requires ext/xcache)● ZendDataCache.php (requires Zend Server Platform)

Page 31: Doctrine Project

Common\Cache

Page 32: Doctrine Project

Common\DataFixtures

Page 33: Doctrine Project

Common\DataFixtures

Page 34: Doctrine Project

Common\Inflector

Page 35: Doctrine Project

DBAL\Migrations● “The Doctrine Migrations offer additional

functionality on top of the database abstraction layer (DBAL) for versioning your database schema and easily deploying changes to it. It is a very easy to use and powerful tool.”

● Instale na munheca... ou... use o PHAR!● 1000000x.... USE O PHAR!

Page 36: Doctrine Project

DBAL\Migrations

Page 37: Doctrine Project

DBAL\Migrations● Configuração

– table_name: doctrine_migration_versions

migrations_directory: /path/to/migrations/classes/DoctrineMigrations

migrations:

migration1:

version: 20100704000000

class: DoctrineMigrations\NewMigration

Page 38: Doctrine Project

DBAL\Migrations● $ doctrine-migrations:generate

Generated new migration class to "/path/to/migrations/classes/DoctrineMigrations/Version20100416130422.php"

Page 39: Doctrine Project

DBAL\Migrations

Page 40: Doctrine Project

DBAL\Migrations

Page 41: Doctrine Project

DBAL\Migrations$ ./doctrine migrations:status

== Configuration

>> Name: Doctrine Sandbox Migrations

>> Database Driver: pdo_mysql

>> Database Name: testdb

>> Configuration Source: /Users/jwage/Sites/doctrine2git/tools/sandbox/migrations.xml

>> Version Table Name: doctrine_migration_versions

>> Migrations Namespace: DoctrineMigrations

>> Migrations Directory: /Users/jwage/Sites/doctrine2git/tools/sandbox/DoctrineMigrations

>> Current Version: 2010-04-16 13:04:22 (20100416130422)

>> Latest Version: 2010-04-16 13:04:22 (20100416130422)

>> Executed Migrations: 0

>> Available Migrations: 1

>> New Migrations: 1

== Migration Versions

>> 2010-04-16 13:04:01 (20100416130401) not migrated

Page 42: Doctrine Project

DBAL\Migrations$ ./doctrine migrations:migrate --dry-run

Are you sure you wish to continue?

y

Executing dry run of migration up to 20100416130452 from 0

>> migrating 20100416130452

-> CREATE TABLE users (username VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL) ENGINE = InnoDB

Page 43: Doctrine Project

DBAL\Migrations$ ./doctrine migrations:migrate 0

Are you sure you wish to continue?

y

Migrating down to 0 from 20100416130422

-- reverting 20100416130422

-> DROP TABLE addresses

-- reverted

-- reverting 20100416130401

-> DROP TABLE users

-- reverted

Page 44: Doctrine Project

Doctrine e outros projetos● Symfony

– Validators

– Router

● Zend Framework 2● Zend Framework 1● JSM\Serializer● DMS\Filter● SuaSempresa\SuaLib

Page 45: Doctrine Project

Como obter ajuda?● Google Groups:

– Doctrine-user

– Doctrine-dev

– Doctrine-BR

● IRC– irc.freenode.net/doctrine

– irc.freenode.net/doctrine-dev

– irc.freenode.net/doctrine-mongodb-odm

● LEIA O MANUAL DE CABO A RABO!

Page 46: Doctrine Project

Contribuindo com o Doctrine● http://www.doctrine-project.org/contribute.html● https://github.com/doctrine

– Há sempre projetos interessantes lá =)

Page 47: Doctrine Project

Reflexões...

Page 48: Doctrine Project

Perguntas?

Page 49: Doctrine Project

Obrigado!