Giter Site home page Giter Site logo

php-di-nextras-orm-extension's Introduction

How to use

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

php-di-nextras-orm-extension's People

Contributors

zarganwar avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.