usnadněte si soužití s doctrine
TRANSCRIPT
![Page 1: Usnadněte si soužití s Doctrine](https://reader034.vdocuments.net/reader034/viewer/2022050805/55700516d8b42a84618b5322/html5/thumbnails/1.jpg)
Usnadněte sisoužití s Doctrine
Filip Procházka
![Page 2: Usnadněte si soužití s Doctrine](https://reader034.vdocuments.net/reader034/viewer/2022050805/55700516d8b42a84618b5322/html5/thumbnails/2.jpg)
Co si ukážeme?
+ Kdyby/Doctrine+ Kdyby/Console+ Kdyby/Events
![Page 3: Usnadněte si soužití s Doctrine](https://reader034.vdocuments.net/reader034/viewer/2022050805/55700516d8b42a84618b5322/html5/thumbnails/3.jpg)
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
![Page 4: Usnadněte si soužití s Doctrine](https://reader034.vdocuments.net/reader034/viewer/2022050805/55700516d8b42a84618b5322/html5/thumbnails/4.jpg)
Instalace
$ composer require \kdyby/doctrine
$ composer create-project \hosiplan/project
![Page 5: Usnadněte si soužití s Doctrine](https://reader034.vdocuments.net/reader034/viewer/2022050805/55700516d8b42a84618b5322/html5/thumbnails/5.jpg)
Konfiguraceextensions: annotations: Kdyby\Annotations\DI\AnnotationsExtension console: Kdyby\Console\DI\ConsoleExtension events: Kdyby\Events\DI\EventsExtension doctrine: Kdyby\Doctrine\DI\OrmExtension
![Page 6: Usnadněte si soužití s Doctrine](https://reader034.vdocuments.net/reader034/viewer/2022050805/55700516d8b42a84618b5322/html5/thumbnails/6.jpg)
Konfigurace
doctrine: user: foo password: bar dbname: sandbox metadata: App: %appDir%
![Page 7: Usnadněte si soužití s Doctrine](https://reader034.vdocuments.net/reader034/viewer/2022050805/55700516d8b42a84618b5322/html5/thumbnails/7.jpg)
Entitause Doctrine\ORM\Mapping as ORM;
/** @ORM\Entity() */
class Article extends \Kdyby\Doctrine\Entities\IdentifiedEntity {
/** @ORM\Column(type="string") */
protected $title;
}
![Page 8: Usnadněte si soužití s Doctrine](https://reader034.vdocuments.net/reader034/viewer/2022050805/55700516d8b42a84618b5322/html5/thumbnails/8.jpg)
BaseEntity? Ta je přece fuj!
class Article extends BaseEntity {
private $one;
protected $two;
public $three;
}
![Page 9: Usnadněte si soužití s Doctrine](https://reader034.vdocuments.net/reader034/viewer/2022050805/55700516d8b42a84618b5322/html5/thumbnails/9.jpg)
Skvělé na prototypování
$article->getOne(); $article->one;$article->getTwo(); $article->two; $article->getThree(); $article->three;
$article->addComment($comment);
$article->removeComment($comment);
![Page 10: Usnadněte si soužití s Doctrine](https://reader034.vdocuments.net/reader034/viewer/2022050805/55700516d8b42a84618b5322/html5/thumbnails/10.jpg)
Striktní validace schématu
$ php www/index.php \orm:validate
![Page 11: Usnadněte si soužití s Doctrine](https://reader034.vdocuments.net/reader034/viewer/2022050805/55700516d8b42a84618b5322/html5/thumbnails/11.jpg)
Entity potřebují tabulky...
$ php www/index.php \orm:schema:up --dump-sql
![Page 12: Usnadněte si soužití s Doctrine](https://reader034.vdocuments.net/reader034/viewer/2022050805/55700516d8b42a84618b5322/html5/thumbnails/12.jpg)
Proxy třídy kvůli lazy loadingu
$ php www/index.php \orm:generate:proxies
![Page 13: Usnadněte si soužití s Doctrine](https://reader034.vdocuments.net/reader034/viewer/2022050805/55700516d8b42a84618b5322/html5/thumbnails/13.jpg)
Repository DAO
$articles = $entityManager
->getDao(App\Article::getClassName());
$articles = $entityManager
->getDao(App\Article::class);
![Page 14: Usnadněte si soužití s Doctrine](https://reader034.vdocuments.net/reader034/viewer/2022050805/55700516d8b42a84618b5322/html5/thumbnails/14.jpg)
Ne, repository nestačí...
$article = new Article();
$article->title = "The Tigger Movie";
$articles->save($article);
$article = $articles->find(1);
echo $article->title; // "The Tigger Movie"
![Page 15: Usnadněte si soužití s Doctrine](https://reader034.vdocuments.net/reader034/viewer/2022050805/55700516d8b42a84618b5322/html5/thumbnails/15.jpg)
Repozitář umí hezky nabobtnat
class ArticlesDao extends EntityDao
{
function findByTitle($title);
function findByStepmothersBrothersEyeColor();
function findBy...();
}
![Page 16: Usnadněte si soužití s Doctrine](https://reader034.vdocuments.net/reader034/viewer/2022050805/55700516d8b42a84618b5322/html5/thumbnails/16.jpg)
Hlavně žádné další dědění!
class ArticlesDao extends EntityDao
{
function findByTitle($title);
function findByStepmothersBrothersEyeColor();
function findBy...();
}
![Page 17: Usnadněte si soužití s Doctrine](https://reader034.vdocuments.net/reader034/viewer/2022050805/55700516d8b42a84618b5322/html5/thumbnails/17.jpg)
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
![Page 18: Usnadněte si soužití s Doctrine](https://reader034.vdocuments.net/reader034/viewer/2022050805/55700516d8b42a84618b5322/html5/thumbnails/18.jpg)
Tak třeba objednávky...
OrderModel - Order\UserOpening- Order\Dispatching- Order\Delivery- ….
![Page 19: Usnadněte si soužití s Doctrine](https://reader034.vdocuments.net/reader034/viewer/2022050805/55700516d8b42a84618b5322/html5/thumbnails/19.jpg)
Eventy: systémové listenery
class EditLogger implements Kdyby\Events\Subscriber {
function getSubscribedEvents() {
return array('postUpdate');
}
function postUpdate() { }
}
![Page 20: Usnadněte si soužití s Doctrine](https://reader034.vdocuments.net/reader034/viewer/2022050805/55700516d8b42a84618b5322/html5/thumbnails/20.jpg)
Eventy: systémové listenery
services:something:
class: App\EditLoggertags: [kdyby.subscriber]
![Page 21: Usnadněte si soužití s Doctrine](https://reader034.vdocuments.net/reader034/viewer/2022050805/55700516d8b42a84618b5322/html5/thumbnails/21.jpg)
ResultSet
$dql = $articles->createQuery("SELECT a FROM Articles a");
$articles = new Kdyby\Doctrine\ResultSet($dql);
$articles->applyPaginator($this['vp']->paginator);
![Page 22: Usnadněte si soužití s Doctrine](https://reader034.vdocuments.net/reader034/viewer/2022050805/55700516d8b42a84618b5322/html5/thumbnails/22.jpg)
Utilitky
Helpers::loadFromFile()Tools\NonLockingUniqueInserter
![Page 23: Usnadněte si soužití s Doctrine](https://reader034.vdocuments.net/reader034/viewer/2022050805/55700516d8b42a84618b5322/html5/thumbnails/23.jpg)
Dotazy?
![Page 24: Usnadněte si soužití s Doctrine](https://reader034.vdocuments.net/reader034/viewer/2022050805/55700516d8b42a84618b5322/html5/thumbnails/24.jpg)
Filip Procházkahttp://filip-prochazka.com
http://www.kdyby.org