PHP code example of zarganwar / php-di-nextras-orm-extension

1. Go to this page and download the library: Download zarganwar/php-di-nextras-orm-extension library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zarganwar / php-di-nextras-orm-extension example snippets


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 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 Zarganwar\PhpDiNextrasOrmExtension\NextrasOrmPhpDiExtension\Attributes\ModelRepository;

#[ModelRepository(AccountRepository::class, 'accounts')]
// ...
// ...
final class Model extends \Nextras\Orm\Model\Model
{

}

// 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),
	),
];

$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