Create your model classes see Nextras/Orm
use Nextras\Orm\Entity\Entity;
/**
* @property-read int $id {primary}
* @property string $name
*/
final class Account extends Entity
{
}
use \Nextras\Orm\Mapper\Mapper;
final class AccountMapper extends Mapper
{
}
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];
}
}
- 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
{
}
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']);
$container->get(AccountRepository::class)->findAll(); // Returns Nextras\Orm\Collection\ICollection