zarganwar/php-di-nextras-orm-extension

There is no license information available for the latest version (1.0.0) of this package.

1.0.0 2024-01-04 17:46 UTC

This package is auto-updated.

Last update: 2024-11-05 17:17:57 UTC


README

Create your model classes see Nextras/Orm

Entities

use Nextras\Orm\Entity\Entity;

/**
 * @property-read int $id {primary}
 * @property string $name
 */
final class Account extends Entity
{

}

Mappers

use \Nextras\Orm\Mapper\Mapper;

final class AccountMapper extends Mapper
{

}

Repositories

Use RepositoryMapper attribute to map repository to mapper class

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!
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

// 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

$containerBuilder = new DI\ContainerBuilder();
$containerBuilder->addDefinitions(__DIR__ . '/config.php');
$build = $containerBuilder->build();

$build->call([OrmExtension::class, 'register']);

Enjoy

$container->get(AccountRepository::class)->findAll(); // Returns Nextras\Orm\Collection\ICollection