Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zarganwar/php-di-nextras-orm-extension
Nextras ORM extension for php-di
https://github.com/zarganwar/php-di-nextras-orm-extension
nextras-orm php-81 php-di php-di-container
Last synced: about 1 month ago
JSON representation
Nextras ORM extension for php-di
- Host: GitHub
- URL: https://github.com/zarganwar/php-di-nextras-orm-extension
- Owner: Zarganwar
- Created: 2024-01-04T17:20:23.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-01-04T17:49:48.000Z (10 months ago)
- Last Synced: 2024-09-29T20:04:21.095Z (about 2 months ago)
- Topics: nextras-orm, php-81, php-di, php-di-container
- Language: PHP
- Homepage:
- Size: 4.88 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## How to use
## Create your model classes see [Nextras/Orm](https://nextras.org/orm)
### Entities
```php
use Nextras\Orm\Entity\Entity;/**
* @property-read int $id {primary}
* @property string $name
*/
final class Account extends Entity
{}
```### Mappers
```php
use \Nextras\Orm\Mapper\Mapper;final class AccountMapper extends Mapper
{}
```### Repositories
Use `RepositoryMapper` attribute to map repository to mapper class
```php
use Zarganwar\PhpDiNextrasOrmExtension\NextrasOrmPhpDiExtension\Attributes\RepositoryMapper;
use Nextras\Orm\Repository\Repository;#[RepositoryMapper(AccountMapper::class)]
final class AccountRepository extends Repository
{public static function getEntityClassNames(): array
{
return [Account::class];
}}
```
### Model
- Use `ModelRepository` attribute to map repository to model class.
- Every repository must be mapped to model class!
- !Do not configure model by [Nextras/Orm - Nette](https://nextras.org/orm/docs/main/config-nette)!```php
use Zarganwar\PhpDiNextrasOrmExtension\NextrasOrmPhpDiExtension\Attributes\ModelRepository;
#[ModelRepository(AccountRepository::class, 'accounts')]
// ...
// ...
final class Model extends \Nextras\Orm\Model\Model
{}
```## Register extension
Use Config class to configure extension
```php
// config.php
use Zarganwar\PhpDiNextrasOrmExtension\NextrasOrmPhpDiExtension\Config;
use Zarganwar\PhpDiNextrasOrmExtension\NextrasOrmPhpDiExtension\OrmExtension;
use Psr\Container\ContainerInterface;return [
// Configure extension
Config::class => fn(ContainerInterface $c) => new Config(
cacheDirectory: __DIR__ . '/../var/cache',
modelClass: Model::class,
connection: [/* See class PhpDoc */]
),// Register extension
OrmExtension::class => fn(ContainerInterface $container) => new OrmExtension(
$container,
$container->get(Config::class),
),
];
```After container build call `OrmExtension::register` method
```php
$containerBuilder = new DI\ContainerBuilder();
$containerBuilder->addDefinitions(__DIR__ . '/config.php');
$build = $containerBuilder->build();$build->call([OrmExtension::class, 'register']);
```## Enjoy
```php
$container->get(AccountRepository::class)->findAll(); // Returns Nextras\Orm\Collection\ICollection
```