Usnadněte si soužití s Doctrine

Post on 04-Jun-2015

1,580 views 0 download

transcript

Usnadněte sisoužití s Doctrine

Filip Procházka

Co si ukážeme?

+ Kdyby/Doctrine+ Kdyby/Console+ Kdyby/Events

Proč Kdyby?

- Doctrine má spoustu skvělých vlastností- Doctrine má spoustu otravných vlastností

- Kdyby je plug&play (convention over configuration), tedy konfigurace na pár řádků a hned můžete pracovat

Instalace

$ composer require \kdyby/doctrine

$ composer create-project \hosiplan/project

Konfiguraceextensions: annotations: Kdyby\Annotations\DI\AnnotationsExtension console: Kdyby\Console\DI\ConsoleExtension events: Kdyby\Events\DI\EventsExtension doctrine: Kdyby\Doctrine\DI\OrmExtension

Konfigurace

doctrine: user: foo password: bar dbname: sandbox metadata: App: %appDir%

Entitause Doctrine\ORM\Mapping as ORM;

/** @ORM\Entity() */

class Article extends \Kdyby\Doctrine\Entities\IdentifiedEntity {

/** @ORM\Column(type="string") */

protected $title;

}

BaseEntity? Ta je přece fuj!

class Article extends BaseEntity {

private $one;

protected $two;

public $three;

}

Skvělé na prototypování

$article->getOne(); $article->one;$article->getTwo(); $article->two; $article->getThree(); $article->three;

$article->addComment($comment);

$article->removeComment($comment);

Striktní validace schématu

$ php www/index.php \orm:validate

Entity potřebují tabulky...

$ php www/index.php \orm:schema:up --dump-sql

Proxy třídy kvůli lazy loadingu

$ php www/index.php \orm:generate:proxies

Repository DAO

$articles = $entityManager

->getDao(App\Article::getClassName());

$articles = $entityManager

->getDao(App\Article::class);

Ne, repository nestačí...

$article = new Article();

$article->title = "The Tigger Movie";

$articles->save($article);

$article = $articles->find(1);

echo $article->title; // "The Tigger Movie"

Repozitář umí hezky nabobtnat

class ArticlesDao extends EntityDao

{

function findByTitle($title);

function findByStepmothersBrothersEyeColor();

function findBy...();

}

Hlavně žádné další dědění!

class ArticlesDao extends EntityDao

{

function findByTitle($title);

function findByStepmothersBrothersEyeColor();

function findBy...();

}

Krocení repozitářů

http://www.whitewashing.de http://www.whitewashing.de/2013/03/04/doctrine_repositories.htmlhttp://filip-prochazka.com/blog/doctrine-a-service-vrstva-aneb-takto-mi-to-dava-smysl

Tak třeba objednávky...

OrderModel - Order\UserOpening- Order\Dispatching- Order\Delivery- ….

Eventy: systémové listenery

class EditLogger implements Kdyby\Events\Subscriber {

function getSubscribedEvents() {

return array('postUpdate');

}

function postUpdate() { }

}

Eventy: systémové listenery

services:something:

class: App\EditLoggertags: [kdyby.subscriber]

ResultSet

$dql = $articles->createQuery("SELECT a FROM Articles a");

$articles = new Kdyby\Doctrine\ResultSet($dql);

$articles->applyPaginator($this['vp']->paginator);

Utilitky

Helpers::loadFromFile()Tools\NonLockingUniqueInserter

Dotazy?

Filip Procházkahttp://filip-prochazka.com

http://www.kdyby.org