### Configure Caching Strategies Source: https://context7.com/doctrine/doctrineormmodule/llms.txt Configure caching strategies for metadata, query, result, and hydration to enhance performance in production environments. This example sets up filesystem and Redis caches. ```php // config/autoload/doctrine.global.php return [ 'doctrine' => [ 'configuration' => [ 'orm_default' => [ 'metadata_cache' => 'filesystem', 'query_cache' => 'filesystem', 'result_cache' => 'redis', 'hydration_cache' => 'redis', ], ], ], ]; ``` -------------------------------- ### Install Doctrine ORM Module Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/README.md Use Composer to install the Doctrine ORM Module for Laminas. ```bash composer require doctrine/doctrine-orm-module ``` -------------------------------- ### Configure Entity Mapping Source: https://context7.com/doctrine/doctrineormmodule/llms.txt Configure entity mapping by registering entity namespaces with metadata drivers. This example uses annotation drivers and specifies the path to your entity classes. ```php // module/Application/config/module.config.php return [ 'doctrine' => [ 'driver' => [ 'application_annotation_driver' => [ 'class' => \Doctrine\ORM\Mapping\Driver\AnnotationDriver::class, 'cache' => 'array', 'paths' => [ __DIR__ . '/../src/Entity', ], ], 'orm_default' => [ 'drivers' => [ 'Application\Entity' => 'application_annotation_driver', ], ], ], ], ]; ``` -------------------------------- ### Configure Database Connection Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/docs/en/user-guide.rst Define connection parameters for the ORM. This example sets up a default MySQL connection named 'orm_default'. ```php [ 'connection' => [ // default connection name 'orm_default' => [ 'driverClass' => \Doctrine\DBAL\Driver\PDO\MySQL\Driver::class, 'params' => [ 'host' => 'localhost', 'port' => '3306', 'user' => 'username', 'password' => 'password', 'dbname' => 'database', ], ], ], ], ]; ``` -------------------------------- ### Install Laminas Developer Tools Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/docs/en/developer-tools.rst Use Composer to install the Laminas Developer Tools package. Ensure Laminas\DeveloperTools is enabled in your modules and profiling/toolbar are configured. ```sh composer require laminas/laminas-developer-tools ``` -------------------------------- ### Configure Entity Metadata Drivers Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/docs/en/user-guide.rst Register entity namespaces with the ORM by adding metadata driver configurations. This example shows how to define an annotation driver and associate it with a namespace. ```php [ 'driver' => [ // defines an annotation driver with two paths, and names it `my_annotation_driver` 'my_annotation_driver' => [ 'class' => \Doctrine\ORM\Mapping\Driver\AnnotationDriver::class, 'cache' => 'array', 'paths' => [ 'path/to/my/entities', 'another/path', ], ], // default metadata driver, aggregates all other drivers into a single one. // Override `orm_default` only if you know what you're doing 'orm_default' => [ 'drivers' => [ // register `my_annotation_driver` for any entity under namespace `My\Namespace` 'My\Namespace' => 'my_annotation_driver', ], ], ], ], ]; ``` -------------------------------- ### Configure Laminas Developer Tools for SQL Logging Source: https://context7.com/doctrine/doctrineormmodule/llms.txt Install Laminas Developer Tools and configure it to log and profile SQL queries for a specific entity manager. This helps in debugging and performance analysis during development. ```php // Install developer tools // composer require laminas/laminas-developer-tools // config/development.config.php return [ 'modules' => [ 'Laminas\DeveloperTools', ], ]; // Configure additional entity manager logging return [ 'doctrine' => [ 'sql_logger_collector' => [ 'orm_other' => [ 'name' => 'orm_other', 'configuration' => 'doctrine.configuration.orm_other', ], ], ], 'laminas-developer-tools' => [ 'profiler' => [ 'collectors' => [ 'orm_other' => 'doctrine.sql_logger_collector.orm_other', ], ], 'toolbar' => [ 'entries' => [ 'orm_other' => 'laminas-developer-tools/toolbar/doctrine-orm', ], ], ], ]; ``` -------------------------------- ### Install DoctrineORMModule via Composer Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/docs/en/index.rst Use this command to add the DoctrineORMModule dependency to your Laminas project. ```bash $ composer require doctrine/doctrine-orm-module ``` -------------------------------- ### Access and Use EntityManager Source: https://context7.com/doctrine/doctrineormmodule/llms.txt Retrieve the EntityManager from the service locator to perform database operations such as finding, creating, persisting, and querying entities. This example demonstrates common ORM operations within a controller. ```php // In a controller or service namespace Application\Controller; use Doctrine\ORM\EntityManager; use Laminas\Mvc\Controller\AbstractActionController; class UserController extends AbstractActionController { public function __construct(private EntityManager $entityManager) { } public function indexAction() { // Find all users $users = $this->entityManager ->getRepository(\Application\Entity\User::class) ->findAll(); // Find by ID $user = $this->entityManager->find(\Application\Entity\User::class, 1); // Create new entity $newUser = new \Application\Entity\User(); $newUser->setName('John Doe'); $newUser->setEmail('john@example.com'); $this->entityManager->persist($newUser); $this->entityManager->flush(); // DQL query $query = $this->entityManager->createQuery( 'SELECT u FROM Application\Entity\User u WHERE u.active = :active' ); $query->setParameter('active', true); $activeUsers = $query->getResult(); return ['users' => $users]; } } ``` -------------------------------- ### Use Doctrine CLI Commands Source: https://context7.com/doctrine/doctrineormmodule/llms.txt Execute various schema, cache, and entity management tasks using the doctrine-module binary. ```bash # Show all available commands ./vendor/bin/doctrine-module # Schema management ./vendor/bin/doctrine-module orm:schema-tool:create ./vendor/bin/doctrine-module orm:schema-tool:update --force ./vendor/bin/doctrine-module orm:schema-tool:drop --force ./vendor/bin/doctrine-module orm:validate-schema # Cache management ./vendor/bin/doctrine-module orm:clear-cache:metadata ./vendor/bin/doctrine-module orm:clear-cache:query ./vendor/bin/doctrine-module orm:clear-cache:result # Entity management ./vendor/bin/doctrine-module orm:info ./vendor/bin/doctrine-module orm:generate-proxies # Run DQL queries ./vendor/bin/doctrine-module orm:run-dql "SELECT u FROM Application\Entity\User u" ``` -------------------------------- ### Configure Authentication Adapter Source: https://context7.com/doctrine/doctrineormmodule/llms.txt Set up Doctrine-based authentication with a custom credential verification callable. ```php use DoctrineModule\\Authentication\\Adapter\\ObjectRepository as DoctrineAdapter; $adapter = new DoctrineAdapter([ 'object_manager' => $entityManager, 'identity_class' => \\Application\\Entity\\User::class, 'identity_property' => 'email', 'credential_property' => 'password', 'credential_callable' => function ($identity, $credential) { return password_verify($credential, $identity->getPassword()); }, ]); $adapter->setIdentity('user@example.com'); $adapter->setCredential('userpassword'); $result = $adapter->authenticate(); if ($result->isValid()) { $user = $result->getIdentity(); echo 'Welcome, ' . $user->getName(); } else { foreach ($result->getMessages() as $message) { echo $message; } } ``` -------------------------------- ### Execute Doctrine Migrations via CLI Source: https://context7.com/doctrine/doctrineormmodule/llms.txt Run migration commands using the doctrine-module binary. ```bash ./vendor/bin/doctrine-module migrations:status ./vendor/bin/doctrine-module migrations:diff ./vendor/bin/doctrine-module migrations:migrate ./vendor/bin/doctrine-module migrations:generate ``` -------------------------------- ### Configure Naming Strategy Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/docs/en/configuration.rst Register and apply a naming strategy to the default ORM configuration. ```php return [ 'service_manager' => [ 'invokables' => [ \Doctrine\ORM\Mapping\UnderscoreNamingStrategy::class => \Doctrine\ORM\Mapping\UnderscoreNamingStrategy::class, ], ], 'doctrine' => [ 'configuration' => [ 'orm_default' => [ 'naming_strategy' => \Doctrine\ORM\Mapping\UnderscoreNamingStrategy::class, ], ], ], ]; ``` -------------------------------- ### Initialize EntityBasedFormBuilder Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/docs/en/forms.rst Configure the form builder using either DocBlock annotations or PHP8 attributes. ```php // using PhpDoc annotations $entityManager = $container->get(\Doctrine\ORM\EntityManager::class); $builder = new \DoctrineORMModule\Form\Annotation\EntityBasedFormBuilder($entityManager); // alternatively, to use PHP8 attributes $entityManager = $container->get(\Doctrine\ORM\EntityManager::class); $attributeBuilder = new \Laminas\Form\Annotation\AttributeBuilder(); $builder = new \DoctrineORMModule\Form\Annotation\EntityBasedFormBuilder($entityManager, $attributeBuilder); ``` -------------------------------- ### Configure Doctrine Migrations Source: https://context7.com/doctrine/doctrineormmodule/llms.txt Define migration storage settings and paths within the doctrine configuration. ```php return [ 'doctrine' => [ 'migrations_configuration' => [ 'orm_default' => [ 'table_storage' => [ 'table_name' => 'doctrine_migration_versions', 'version_column_name' => 'version', 'version_column_length' => 191, 'executed_at_column_name' => 'executed_at', 'execution_time_column_name' => 'execution_time', ], 'migrations_paths' => [ 'Application\Migrations' => 'module/Application/src/Migrations', ], 'all_or_nothing' => true, 'check_database_platform' => true, 'organize_migrations' => 'year_and_month', ], ], ], ]; ``` -------------------------------- ### Configure Multiple Entity Managers Source: https://context7.com/doctrine/doctrineormmodule/llms.txt Defines separate connections, configurations, and drivers for multiple entity managers. ```php return [ 'doctrine' => [ 'connection' => [ 'orm_crawler' => [ 'driverClass' => \Doctrine\DBAL\Driver\PDO\MySQL\Driver::class, 'eventmanager' => 'orm_crawler', 'configuration' => 'orm_crawler', 'params' => [ 'host' => 'localhost', 'port' => '3306', 'user' => 'crawler_user', 'password' => 'crawler_pass', 'dbname' => 'crawler_db', ], ], ], 'configuration' => [ 'orm_crawler' => [ 'metadata_cache' => 'array', 'query_cache' => 'array', 'result_cache' => 'array', 'hydration_cache' => 'array', 'driver' => 'orm_crawler_chain', 'generate_proxies' => true, 'proxy_dir' => 'data/DoctrineORMModule/Proxy', 'proxy_namespace' => 'DoctrineORMModule\Proxy', ], ], 'driver' => [ 'orm_crawler_annotation' => [ 'class' => \Doctrine\ORM\Mapping\Driver\AnnotationDriver::class, 'cache' => 'array', 'paths' => [__DIR__ . '/../src/Crawler/Entity'], ], 'orm_crawler_chain' => [ 'class' => \Doctrine\Persistence\Mapping\Driver\MappingDriverChain::class, 'drivers' => [ 'Crawler\Entity' => 'orm_crawler_annotation', ], ], ], 'entitymanager' => [ 'orm_crawler' => [ 'connection' => 'orm_crawler', 'configuration' => 'orm_crawler', ], ], 'eventmanager' => [ 'orm_crawler' => [], ], ], ]; // Usage in service $defaultEm = $container->get('doctrine.entitymanager.orm_default'); $crawlerEm = $container->get('doctrine.entitymanager.orm_crawler'); ``` -------------------------------- ### Enable and Configure Second Level Cache Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/docs/en/cache.rst Enable the Second Level Cache and configure its default and region-specific settings, including cache lifetimes and file lock directories. This configuration reuses the cache defined for the result cache. ```php return [ 'doctrine' => [ 'configuration' => [ 'orm_default' => [ 'result_cache' => 'redis', // Second level cache reuse the cache defined in result cache 'second_level_cache' => [ 'enabled' => true, 'default_lifetime' => 200, 'default_lock_lifetime' => 500, 'file_lock_region_directory' => __DIR__ . '/../my_dir', 'regions' => [ 'My\FirstRegion\Name' => [ 'lifetime' => 800, 'lock_lifetime' => 1000, ], 'My\SecondRegion\Name' => [ 'lifetime' => 10, 'lock_lifetime' => 20, ], ], ], ], ], ], ]; ``` -------------------------------- ### Configure Quote Strategy Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/docs/en/configuration.rst Register and apply a quote strategy to the default ORM configuration. ```php return [ 'service_manager' => [ 'invokables' => [ \Doctrine\ORM\Mapping\AnsiQuoteStrategy::class => \Doctrine\ORM\Mapping\AnsiQuoteStrategy::class, ], ], 'doctrine' => [ 'configuration' => [ 'orm_default' => [ 'quote_strategy' => \Doctrine\ORM\Mapping\AnsiQuoteStrategy::class, ], ], ], ]; ``` -------------------------------- ### Configure Default Cache Adapters Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/docs/en/cache.rst Set different cache adapters for query, result, metadata, and hydration caches in your configuration. The array cache is suitable for development only. ```php return [ 'doctrine' => [ 'configuration' => [ 'orm_default' => [ 'query_cache' => 'filesystem', 'result_cache' => 'array', 'metadata_cache' => 'apc', 'hydration_cache' => 'memcached', ], ], ], ]; ``` -------------------------------- ### Redis Factory Implementation Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/docs/en/cache.rst A factory class to create and configure a Redis cache instance. It connects to the Redis server at the default host and port. ```php namespace Db\Cache; use Psr\Container\ContainerInterface; use Redis; class RedisFactory { public function __invoke( ContainerInterface $container, $requestedName, array $options = null ) { $redis = new Redis(); $redis->connect('127.0.0.1', 6379); return $redis; } } ``` -------------------------------- ### Configure Multiple Connections Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/docs/en/configuration.rst Define additional ORM connections by specifying unique keys in the connection and configuration arrays. ```php return [ 'doctrine' => [ 'connection' => [ 'orm_crawler' => [ 'driverClass' => \Doctrine\DBAL\Driver\PDO\MySQL\Driver::class, 'eventmanager' => 'orm_crawler', 'configuration' => 'orm_crawler', 'params' => [ 'host' => 'localhost', 'port' => '3306', 'user' => 'root', 'password' => 'root', 'dbname' => 'crawler', 'driverOptions' => [ 1002 => 'SET NAMES utf8', ], ], ], ], 'configuration' => [ 'orm_crawler' => [ 'metadata_cache' => 'array', 'query_cache' => 'array', 'result_cache' => 'array', 'hydration_cache' => 'array', 'driver' => 'orm_crawler_chain', ``` -------------------------------- ### Configure Naming and Quote Strategies Source: https://context7.com/doctrine/doctrineormmodule/llms.txt Set naming and quote strategies in the doctrine configuration to control how entity properties map to database columns. ```php return [ 'service_manager' => [ 'invokables' => [ \Doctrine\ORM\Mapping\UnderscoreNamingStrategy::class => \Doctrine\ORM\Mapping\UnderscoreNamingStrategy::class, \Doctrine\ORM\Mapping\AnsiQuoteStrategy::class => \Doctrine\ORM\Mapping\AnsiQuoteStrategy::class, ], ], 'doctrine' => [ 'configuration' => [ 'orm_default' => [ 'naming_strategy' => \Doctrine\ORM\Mapping\UnderscoreNamingStrategy::class, 'quote_strategy' => \Doctrine\ORM\Mapping\AnsiQuoteStrategy::class, ], ], ], ]; ``` -------------------------------- ### Access Doctrine Command Line Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/docs/en/user-guide.rst Execute Doctrine commands using the provided command-line interface script. ```sh ./vendor/bin/doctrine-module ``` -------------------------------- ### Integrate Doctrine Paginator Source: https://context7.com/doctrine/doctrineormmodule/llms.txt Use DoctrinePaginator to handle pagination for Laminas applications. ```php use Doctrine\\ORM\\Tools\\Pagination\\Paginator as ORMPaginator; use DoctrineORMModule\\Paginator\\Adapter\\DoctrinePaginator; use Laminas\\Paginator\\Paginator; // Create DQL query $query = $entityManager->createQuery( 'SELECT u FROM Application\\Entity\\User u WHERE u.active = :active ORDER BY u.createdAt DESC' ); $query->setParameter('active', true); // Create paginator $ormPaginator = new ORMPaginator($query); $adapter = new DoctrinePaginator($ormPaginator); $paginator = new Paginator($adapter); // Configure pagination $paginator->setCurrentPageNumber($page); $paginator->setItemCountPerPage(25); // Use in view foreach ($paginator as $user) { echo $user->getName(); } // Get pagination info $totalItems = $paginator->getTotalItemCount(); $totalPages = $paginator->count(); ``` -------------------------------- ### Configure DBAL 3.x Middlewares Source: https://context7.com/doctrine/doctrineormmodule/llms.txt Configure DBAL 3.x middlewares for logging, profiling, or custom connection handling. This involves defining the middleware services and adding them to the Doctrine configuration. ```php return [ 'service_manager' => [ 'invokables' => [ \My\Middlewares\LoggingMiddleware::class => \My\Middlewares\LoggingMiddleware::class, \My\Middlewares\ProfilingMiddleware::class => \My\Middlewares\ProfilingMiddleware::class, ], ], 'doctrine' => [ 'configuration' => [ 'orm_default' => [ 'middlewares' => [ \My\Middlewares\LoggingMiddleware::class, \My\Middlewares\ProfilingMiddleware::class, ], ], ], ], ]; ``` -------------------------------- ### Implement Doctrine Form Elements Source: https://context7.com/doctrine/doctrineormmodule/llms.txt Populate form selects and checkboxes using entity data. ```php use Laminas\\Form\\Form; use DoctrineModule\\Form\\Element\\ObjectSelect; use DoctrineModule\\Form\\Element\\ObjectMultiCheckbox; class ArticleForm extends Form { public function init() { $this->add([ 'type' => ObjectSelect::class, 'name' => 'category', 'options' => [ 'label' => 'Category', 'object_manager' => $this->entityManager, 'target_class' => \\Application\\Entity\\Category::class, 'property' => 'name', 'is_method' => true, 'find_method' => [ 'name' => 'findBy', 'params' => [ 'criteria' => ['active' => true], 'orderBy' => ['name' => 'ASC'], ], ], 'empty_option' => '-- Select Category --', ], ]); $this->add([ 'type' => ObjectMultiCheckbox::class, 'name' => 'tags', 'options' => [ 'label' => 'Tags', 'object_manager' => $this->entityManager, 'target_class' => \\Application\\Entity\\Tag::class, 'property' => 'name', 'label_generator' => function ($entity) { return $entity->getName() . ' (' . $entity->getCount() . ')'; }, ], ]); } } ``` -------------------------------- ### Override RunSqlCommand Factory Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/docs/en/configuration.rst Replace the default factory for the RunSqlCommand console command. ```php return [ 'service_manager' => [ 'factories' => [ 'doctrine.dbal_cmd.runsql' => MyCustomRunSqlCommandFactory::class, ], ], ]; ``` -------------------------------- ### Configure Entity Resolvers Source: https://context7.com/doctrine/doctrineormmodule/llms.txt Use the entity_resolver configuration to map interfaces to concrete entity classes. ```php return [ 'doctrine' => [ 'entity_resolver' => [ 'orm_default' => [ 'resolvers' => [ \Acme\InvoiceModule\Model\InvoiceSubjectInterface::class => \Acme\CustomerModule\Entity\Customer::class, \Acme\Contracts\UserInterface::class => \Application\Entity\User::class, ], ], ], ], ]; ``` -------------------------------- ### Configure DBAL Middlewares Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/docs/en/configuration.rst Register middlewares in the service manager and assign them to the ORM configuration. This feature requires DBAL 3.x. ```php return [ 'service_manager' => [ 'invokables' => [ \My\Middlewares\CustomMiddleware::class => \My\Middlewares\CustomMiddleware::class, \My\Middlewares\AnotherCustomMiddleware::class => \My\Middlewares\AnotherCustomMiddleware::class, ], ], 'doctrine' => [ 'configuration' => [ 'test_default' => [ 'middlewares' => [ \My\Middlewares\CustomMiddleware::class, \My\Middlewares\AnotherCustomMiddleware::class, ], ], ], ], ]; ``` -------------------------------- ### Enable Second Level Cache Source: https://context7.com/doctrine/doctrineormmodule/llms.txt Configures the second level cache settings within the Doctrine configuration array. ```php return [ 'doctrine' => [ 'configuration' => [ 'orm_default' => [ 'result_cache' => 'redis', 'second_level_cache' => [ 'enabled' => true, 'default_lifetime' => 3600, 'default_lock_lifetime' => 60, 'file_lock_region_directory' => 'data/cache/doctrine', 'regions' => [ 'My\Entity\Region' => [ 'lifetime' => 7200, 'lock_lifetime' => 120, ], ], ], ], ], ], ]; ``` -------------------------------- ### Configure Redis Cache Adapter Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/docs/en/cache.rst Configure Doctrine ORM to use Redis for query, result, metadata, and hydration caches. This involves setting up a service factory for the Redis instance and referencing it in the cache configuration. ```php namespace Db; return [ 'service_manager' => [ 'factories' => [ 'Db\Cache\Redis' => Db\Cache\RedisFactory::class, ], ], 'doctrine' => [ 'cache' => [ 'redis' => [ 'namespace' => 'Db_Doctrine', 'instance' => 'Db\Cache\Redis', ], ], 'configuration' => [ 'orm_default' => [ 'query_cache' => 'redis', 'result_cache' => 'redis', 'metadata_cache' => 'redis', 'hydration_cache' => 'redis', ], ], ], ]; ``` -------------------------------- ### Set a Custom Default Repository Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/docs/en/configuration.rst Define a custom repository class name to be used as the default for ORM entities. ```php return [ 'doctrine' => [ 'configuration' => [ 'orm_default' => [ 'default_repository_class_name' => 'MyCustomRepository', ], ], ], ]; ``` -------------------------------- ### Generate Forms from Entities Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/docs/en/forms.rst Create a form specification or a fully instantiated form object from an entity instance. ```php $entity = new User(); // get form specification only $formSpec = $builder->getFormSpecification($entity); // or directly get form $form= $builder->createForm($entity); ``` -------------------------------- ### Configure Redis Cache for Doctrine Source: https://context7.com/doctrine/doctrineormmodule/llms.txt Registers a custom Redis factory in the service manager and assigns it to Doctrine cache types. ```php // module/Db/config/module.config.php namespace Db; return [ 'service_manager' => [ 'factories' => [ 'Db\Cache\Redis' => Db\Cache\RedisFactory::class, ], ], 'doctrine' => [ 'cache' => [ 'redis' => [ 'namespace' => 'Db_Doctrine', 'instance' => 'Db\Cache\Redis', ], ], 'configuration' => [ 'orm_default' => [ 'query_cache' => 'redis', 'result_cache' => 'redis', 'metadata_cache' => 'redis', 'hydration_cache' => 'redis', ], ], ], ]; // module/Db/src/Cache/RedisFactory.php namespace Db\Cache; use Psr\Container\ContainerInterface; use Redis; class RedisFactory { public function __invoke(ContainerInterface $container): Redis { $redis = new Redis(); $redis->connect('127.0.0.1', 6379); return $redis; } } ``` -------------------------------- ### Use Specific Object Manager via CLI Source: https://context7.com/doctrine/doctrineormmodule/llms.txt Specify a custom object manager for Doctrine CLI commands. ```bash ./vendor/bin/doctrine-module orm:info --object-manager=doctrine.entitymanager.orm_crawler ``` -------------------------------- ### Configure Doctrine Object Authentication Adapter Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/docs/en/miscellaneous.rst Use this adapter for Laminas Authentication. Provide the entity manager, entity name, identity field, and credential field. An optional callable can be provided for password hashing. ```php getSalt(), $identity->getAlgorithm() ); } ); $adapter->setIdentityValue('admin'); $adapter->setCredentialValue('password'); $result = $adapter->authenticate(); echo $result->isValid() ? 'Authenticated' : 'Could not authenticate'; ``` -------------------------------- ### Create Forms with EntityBasedFormBuilder Source: https://context7.com/doctrine/doctrineormmodule/llms.txt Generate forms automatically from entity metadata or PHP8 attributes. ```php use DoctrineORMModule\\Form\\Annotation\\EntityBasedFormBuilder; use Doctrine\\ORM\\EntityManager; // Create the form builder $entityManager = $container->get(EntityManager::class); $builder = new EntityBasedFormBuilder($entityManager); // Build form from entity $entity = new \\Application\\Entity\\User(); $form = $builder->createForm($entity); // Or get form specification for customization $formSpec = $builder->getFormSpecification($entity); // Using PHP8 attributes instead of annotations $attributeBuilder = new \\Laminas\\Form\\Annotation\\AttributeBuilder(); $builder = new EntityBasedFormBuilder($entityManager, $attributeBuilder); $form = $builder->createForm($entity); // Attach custom event listeners $myListener = new MyFormListener(); $myListener->attach($builder->getBuilder()->getEventManager()); ``` -------------------------------- ### Configure Doctrine Migrations Table Storage Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/docs/en/migrations.rst Set up the table storage details for Doctrine Migrations, including table name, version column name and length, and executed timestamp columns. This configuration is essential for managing migration history. ```php return [ 'doctrine' => [ 'migrations_configuration' => [ 'orm_default' => [ 'table_storage' => [ 'table_name' => 'DoctrineMigrationVersions', 'version_column_name' => 'version', 'version_column_length' => 191, 'executed_at_column_name' => 'executedAt', 'execution_time_column_name' => 'executionTime', ], 'migrations_paths' => [], // an array of namespace => path 'migrations' => [], // an array of fully qualified migrations 'all_or_nothing' => false, 'check_database_platform' => true, 'organize_migrations' => 'year', // year or year_and_month 'custom_template' => null, ], 'orm_other' => [ ... ] ], ], ]; ``` -------------------------------- ### Define Relationships with ResolveTargetEntityListener Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/docs/en/configuration.rst Use the entity_resolver configuration to map interfaces or abstract classes to concrete entity implementations. ```php return [ 'doctrine' => [ 'entity_resolver' => [ 'orm_default' => [ 'resolvers' => [ \Acme\InvoiceModule\Model\InvoiceSubjectInterface::class, \Acme\CustomerModule\Entity\Customer::class, ], ], ], ], ]; ``` -------------------------------- ### Customize Form Builder Components Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/docs/en/forms.rst Access the event manager or form factory to attach listeners or configure the form element manager. ```php // if you need access to the event manager $myListener = new MyListener(); $myListener->attach($builder->getBuilder()->getEventManager()); // if you need access to the form factory $formElementManager = $container->get(\Laminas\Form\FormElementManager::class) $builder->getBuilder()->getFormFactory()->setFormElementManager($formElementManager); ``` -------------------------------- ### Configure Database Connection Source: https://context7.com/doctrine/doctrineormmodule/llms.txt Configure the default database connection for Doctrine ORM, specifying driver, host, credentials, and database name. This configuration is typically placed in a local configuration file. ```php // config/autoload/doctrine.local.php return [ 'doctrine' => [ 'connection' => [ 'orm_default' => [ 'driverClass' => \Doctrine\DBAL\Driver\PDO\MySQL\Driver::class, 'params' => [ 'host' => 'localhost', 'port' => '3306', 'user' => 'username', 'password' => 'password', 'dbname' => 'database', 'charset' => 'utf8mb4', 'driverOptions' => [ 1002 => 'SET NAMES utf8mb4', ], ], ], ], ], ]; ``` -------------------------------- ### Register Doctrine ORM Module Source: https://context7.com/doctrine/doctrineormmodule/llms.txt Register the DoctrineModule and DoctrineORMModule in your Laminas application's configuration file to enable them. ```php // config/application.config.php return [ 'modules' => [ 'DoctrineModule', 'DoctrineORMModule', // ... your other modules ], ]; ``` -------------------------------- ### Register a Custom DBAL Type Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/docs/en/configuration.rst Register a custom type implementation with the ORM configuration and map it to the database platform. ```php return [ 'doctrine' => [ 'configuration' => [ 'orm_default' => [ 'types' => [ 'newtype' => \My\Types\NewType::class, ], ], ], ], ]; ``` ```php return [ 'doctrine' => [ 'connection' => [ 'orm_default' => [ 'doctrine_type_mappings' => [ 'mytype' => 'mytype', ], ], ], ], ]; ``` -------------------------------- ### Map Custom DBAL Types to Database Platform Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/docs/en/miscellaneous.rst Register custom DBAL type mappings with your database platform to ensure Schema-Tool converts underlying database types correctly. ```php [ 'connection' => [ 'orm_default' => [ 'doctrine_type_mappings' => [ 'tinyint' => 'tinyint', ], ], ], ], ]; ``` -------------------------------- ### Configure Multiple EntityManagers for Logging Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/docs/en/developer-tools.rst This PHP code configures logging for an additional DBAL Connection or EntityManager. It defines a SQL logger collector and registers it with Laminas\DeveloperTools for profiling and toolbar display. ```php [ 'sql_logger_collector' => [ 'other_orm' => [ // name of the sql logger collector (used by Laminas\DeveloperTools) 'name' => 'other_orm', // name of the configuration service at which to attach the logger 'configuration' => 'doctrine.configuration.other_orm', // uncomment following if you want to use a particular SQL logger instead of relying on // the attached one //'sql_logger' => 'service_name_of_my_dbal_sql_logger', ], ], ], 'laminas-developer-tools' => [ // registering the profiler with Laminas\DeveloperTools 'profiler' => [ 'collectors' => [ // reference to the service we have defined 'other_orm' => 'doctrine.sql_logger_collector.other_orm', ], ], // registering a new toolbar item with Laminas\DeveloperTools (name must be the same of the collector name) 'toolbar' => [ 'entries' => [ // this is actually a name of a view script to use - you can use your custom one 'other_orm' => 'laminas-developer-tools/toolbar/doctrine-orm', ], ], ], ]; } public function getServiceConfiguration() { return [ 'factories' => [ // defining a service (any name is valid as long as you use it consistently across this example) 'doctrine.sql_logger_collector.other_orm' => new \DoctrineORMModule\Service\SQLLoggerCollectorFactory('other_orm'), ], ]; } public function onBootstrap(\Laminas\EventManager\EventInterface $e) { $config = $e->getTarget()->getServiceManager()->get('Config'); if (isset($config['laminas-developer-tools']['profiler']['enabled']) && $config['laminas-developer-tools']['profiler']['enabled'] ) { // when Laminas\DeveloperTools is enabled, initialize the sql collector $app->getServiceManager()->get('doctrine.sql_logger_collector.other_orm'); } } } ``` -------------------------------- ### Register Custom DBAL Types Source: https://context7.com/doctrine/doctrineormmodule/llms.txt Map custom DBAL types in the configuration and apply them to entity properties using the ORM Column attribute. ```php // config/autoload/doctrine.global.php return [ 'doctrine' => [ 'configuration' => [ 'orm_default' => [ 'types' => [ 'money' => \My\DBAL\Types\MoneyType::class, 'tinyint' => \My\DBAL\Types\TinyIntType::class, ], ], ], 'connection' => [ 'orm_default' => [ 'doctrine_type_mappings' => [ 'tinyint' => 'tinyint', ], ], ], ], ]; // Entity usage use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] class Product { #[ORM\Column(type: 'money')] private string $price; #[ORM\Column(type: 'tinyint')] private int $quantity; } ``` -------------------------------- ### Register a Custom DQL Function Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/docs/en/configuration.rst Add custom DQL functions to the ORM configuration under the numeric_functions key. ```php return [ 'doctrine' => [ 'configuration' => [ 'orm_default' => [ 'numeric_functions' => [ 'ROUND' => \My\DoctrineExtensions\Query\Mysql\Round::class, ], ], ], ], ]; ``` -------------------------------- ### Set Custom Dependency Factory Service for Migrations Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/docs/en/migrations.rst Configure Doctrine Migrations to use a custom service ID for a specific dependency factory service. Ensure the custom service ID is defined in your DIC. This allows for overriding default components like the version comparator. ```php return [ 'doctrine' => [ 'migrations_configuration' => [ 'orm_default' => [ 'dependency_factory_services' => [ 'service_to_overwrite' => 'custom_service_id' ], ], ], ], ]; ``` ```php return [ 'doctrine' => [ 'migrations_configuration' => [ 'orm_default' => [ 'dependency_factory_services' => [ \Doctrine\Migrations\Version\Comparator::class => MyComparator::class ], ], ], ], ]; ``` -------------------------------- ### Register Custom DQL Functions Source: https://context7.com/doctrine/doctrineormmodule/llms.txt Define custom numeric, string, and datetime functions in the doctrine configuration array. These functions can then be used directly within DQL queries. ```php return [ 'doctrine' => [ 'configuration' => [ 'orm_default' => [ 'numeric_functions' => [ 'ROUND' => \My\DoctrineExtensions\Query\Mysql\Round::class, 'RAND' => \My\DoctrineExtensions\Query\Mysql\Rand::class, ], 'string_functions' => [ 'GROUP_CONCAT' => \My\DoctrineExtensions\Query\Mysql\GroupConcat::class, ], 'datetime_functions' => [ 'DATE_FORMAT' => \My\DoctrineExtensions\Query\Mysql\DateFormat::class, ], ], ], ], ]; // Usage in DQL $query = $entityManager->createQuery( 'SELECT u, ROUND(u.score, 2) as roundedScore FROM Application\Entity\User u ORDER BY RAND()' ); ``` -------------------------------- ### Access Entity Manager via Service Locator Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/docs/en/user-guide.rst Retrieve the Doctrine Entity Manager instance from the application's service locator. This is commonly done within controllers or other application services. ```php // for example, in a controller: $em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default'); $em = $this->getServiceLocator()->get(\Doctrine\ORM\EntityManager::class); ``` -------------------------------- ### Register Custom DBAL Types in Configuration Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/docs/en/miscellaneous.rst Add custom Doctrine DBAL types to the 'doctrine.configuration.orm_default.types' key in your configuration file to override or define new types. ```php [ 'configuration' => [ 'orm_default' => [ 'types' => [ // You can override a default type 'date' => 'My\DBAL\Types\DateType', // And set new ones 'tinyint' => 'My\DBAL\Types\TinyIntType', ], ], ], ], ]; ``` -------------------------------- ### Exclude Tables from Schema Diff Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/docs/en/configuration.rst Use a filter callback to prevent specific tables from being dropped during schema updates. ```php return [ 'doctrine' => [ 'configuration' => [ 'orm_default' => [ 'schema_assets_filter' => fn (string $tableName): bool => ( ! in_array($tableName, ['doNotRemoveThisTable', 'alsoDoNotRemoveThisTable']) ), ], ], ], ]; ``` -------------------------------- ### Define ORM Entity Field Datatypes Source: https://github.com/doctrine/doctrineormmodule/blob/6.4.x/docs/en/miscellaneous.rst Use custom DBAL types like 'date' and 'tinyint' in your ORM entities to define field datatypes. ```php [ 'configuration' => [ 'orm_default' => [ 'schema_assets_filter' => function (string $tableName): bool { $excludedTables = [ 'legacy_users', 'external_data', 'migration_versions', ]; return !in_array($tableName, $excludedTables, true); }, ], ], ], ]; // For cacheable config, use static method return [ 'doctrine' => [ 'configuration' => [ 'orm_default' => [ 'schema_assets_filter' => [\Application\Filter\SchemaFilter::class, 'filter'], ], ], ], ]; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.