https://github.com/kocal/zend-expressive-doctrinedatabase
Use Doctrine ORM in Zend-Expressive
https://github.com/kocal/zend-expressive-doctrinedatabase
database doctrine-orm zend-expressive zend-expressive-database
Last synced: about 2 months ago
JSON representation
Use Doctrine ORM in Zend-Expressive
- Host: GitHub
- URL: https://github.com/kocal/zend-expressive-doctrinedatabase
- Owner: Kocal
- Created: 2017-08-05T20:56:03.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-08-05T21:43:42.000Z (almost 9 years ago)
- Last Synced: 2025-01-16T23:07:41.674Z (over 1 year ago)
- Topics: database, doctrine-orm, zend-expressive, zend-expressive-database
- Language: PHP
- Homepage:
- Size: 13.7 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Zend-Expressive Doctrine Database
=================================
# Installation
## Composer
Run `composer require kocal/zend-expressive-doctrinedatabase`
## Zend-Expressive configuration
In a Zend-Expressive configuration file (e.g.: `config/autoload/database.global.php` if you used Zend-Expressive app generator):
```php
[
'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`
],
];
```
Now let's create an Entity and its Repository.
## Usage
### Creating an Entity
Let's say our Entities are located in `src/App/Entity` folder.
Example of a `Post` Entity:
```php
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;
}
}
```
### Creating a Repository
A Repository should extends from [`DoctrineRepository`](./src/DoctrineRepository.php) class, which implements [`DatabaseRepositoryInterface`](https://github.com/Kocal/zend-expressive-database/blob/master/src/DatabaseRepositoryInterface.php)
```php
createQueryBuilder('p')
->select('p')
->setMaxResults(2)
->orderBy('p.id', 'DESC')
->getQuery()
->getResult();
}
}
```
### Using a Repository:
```php
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);
```