Zend Doctrine Introduction

What is Doctrine ORM?

Doctrine ORM (Object-Relational Mapper) is a powerful library for managing database operations using PHP objects instead of writing raw SQL.



Why Use Doctrine?

Feature

Explanation

Object Mapping

Map database tables to PHP classes (Entities).

Unit Testing

Easier to test with object abstraction.

Less SQL

Reduces manual SQL queries.

Migrations

Manage DB schema changes using version control.


Installing Doctrine ORM and Migrations

composer require doctrine/doctrine-module doctrine/doctrine-orm-module doctrine/migrations


These packages include:

  • DoctrineModule: Integration with Laminas (Zend).
  • DoctrineORMModule: ORM functionality (entities, relations, DQL).
  • Doctrine Migrations: For schema versioning.

Enable Doctrine Modules in Zend

Edit modules.config.php(config\modules.config.php)

return [
  'Laminas\Router',
  'Laminas\Validator',
  'DoctrineModule',
  'DoctrineORMModule',
  'Application',
];

Create Migration Folder

Create a new folder under /data:

mkdir data\Migrations

Database Credentials Configuration (doctrine.global.php(config\autoload\doctrine.global.php)

In Laminas, Doctrine database connection credentials are typically stored in a global config file called doctrine.global.php.

use Doctrine\DBAL\Driver\PDO\MySQL\Driver;

return [
  'doctrine' => [
    'connection' => [
      'orm_default' => [
        'driverClass' => Driver::class,
        'params' => [
          'host'   => '127.0.0.1',
          'port'   => '3306',
          'user'   => 'root',
          'password' => '',
          'dbname'  => 'zend',
          'charset' => 'utf8mb4',
        ],
      ],
    ],
  ],
];

Configure Doctrine Driver (Entity Mapping) (module/Application/config/module.config.php)

return [
  'doctrine' => [
    'driver' => [
      'application_entities' => [
        'class' => Doctrine\ORM\Mapping\Driver\AnnotationDriver::class,
        'cache' => 'array',
        'paths' => [__DIR__ . '/../src/Entity'],
      ],
      'orm_default' => [
        'drivers' => [
          'Application\Entity' => 'application_entities',
        ],
      ],
    ],
  ],
];

Configure Doctrine Driver (Entity Mapping) (module/Application/config/module.config.php)

return [
  'doctrine' => [
    'driver' => [
      'application_entities' => [
        'class' => Doctrine\ORM\Mapping\Driver\AnnotationDriver::class,
        'cache' => 'array',
        'paths' => [__DIR__ . '/../src/Entity'],
      ],
      'orm_default' => [
        'drivers' => [
          'Application\Entity' => 'application_entities',
        ],
      ],
    ],
  ],
];

Setup CLI for Migrations

File create cli-config.php inside project folder 

use Doctrine\Migrations\DependencyFactory;
use Doctrine\Migrations\Configuration\Migration\PhpFile;
use Doctrine\Migrations\Configuration\EntityManager\ExistingEntityManager;

require 'vendor/autoload.php';
$container = require 'config/container.php';

return DependencyFactory::fromEntityManager(
  new PhpFile('migrations.php'),
  new ExistingEntityManager($container->get(\Doctrine\ORM\EntityManager::class))
);

Define Migrations Config

File create migrations.php inside project folder 

return [
  'table_storage' => [
    'table_name' => 'doctrine_migration_versions',
  ],
  'migrations_paths' => [
    'Application\Migrations' => 'data/Migrations',
  ],
];

Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.