kocal / zend-expressive-doctrinedatabase
v1.0
2017-08-05 21:42 UTC
Requires
- doctrine/orm: 2.4.*
- kocal/zend-expressive-database: ^1.0
- psr/container: ^1.0
Requires (Dev)
- phpunit/phpunit: ^6.3
This package is auto-updated.
Last update: 2024-05-17 18:06:29 UTC
README
安装
Composer
运行composer require kocal/zend-expressive-doctrinedatabase
Zend-Expressive配置
在Zend-Expressive配置文件中(例如:如果你使用了Zend-Expressive应用生成器,则是config/autoload/database.global.php)
<?php use Doctrine\ORM\EntityManager; use Kocal\Expressive\Database\Doctrine\EntityManagerFactory; return [ 'dependencies' => [ 'factories' => [ // Use EntityManagerFactory for using Doctrine EntityManager: EntityManager::class => EntityManagerFactory::class ] ], 'doctrine' => [ // DBAL configuration. More at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html 'driver' => 'pdo_sqlite', 'path' => __DIR__ . '/../../database/database.sqlite', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', ], 'entities_path' => [ // Path to Entity, e.g.: `/path/to/project/src/App/Entity` ], ];
现在让我们创建一个实体及其存储库。
用法
创建实体
假设我们的实体位于src/App/Entity文件夹中。
Post实体的示例
<?php namespace App\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity(repositoryClass="App\Repository\PostRepository") * @ORM\Table(name="posts") */ class Post { /** * @ORM\Id() * @ORM\Column(type="integer") * @ORM\GeneratedValue() */ private $id; /** * @var string * @ORM\Column(name="title", type="string") */ private $title; /** * @var string * @ORM\Column(name="content", type="text") */ private $content; /** * Post constructor. * @param string $title * @param string $content */ public function __construct($title, $content) { $this->title = $title; $this->content = $content; } /** * @return mixed */ public function getId() { return $this->id; } /** * @param mixed $id */ public function setId($id) { $this->id = $id; } /** * @return string */ public function getTitle() { return $this->title; } /** * @param string $title */ public function setTitle($title) { $this->title = $title; } /** * @return string */ public function getContent() { return $this->content; } /** * @param string $content */ public function setContent($content) { $this->content = $content; } }
创建存储库
存储库应该扩展自DoctrineRepository类,它实现了DatabaseRepositoryInterface接口。
<?php namespace App\Repository; use App\Entity\Post; use Kocal\Expressive\Database\Doctrine\DoctrineRepository; /** * Class PostRepository */ class PostRepository extends DoctrineRepository { // Some methods are already implemented. // Do not hesitate to read `DoctrineRepository.php`! // You can add custom methods /** * @return Post[] */ public function getTwoLastPosts() { return $this->createQueryBuilder('p') ->select('p') ->setMaxResults(2) ->orderBy('p.id', 'DESC') ->getQuery() ->getResult(); } }
使用存储库
<?php use App\Entity\Post; use Doctrine\ORM\EntityManager; // Retrieve EntityManager in the Container $em = $container->get(EntityManager::class); // Retrieve PostRepository $postRepository = $em->getRepository(Post::class); // Use it! $allPosts = $postRepository->all(); $firstPost = $postRepository->first(); $lastPost = $postRepository->last(); $post = new Post('Hello world!', 'Lorem ispum dolor sit amet...'); $postRepository->save($post);