Advertisement

Google Ad Slot: content-top

Zend Migration


What is Doctrine Migration?

Doctrine Migrations allow tracking, versioning, and updating your database schema. Think of it like Git for your database.


Why Use Migrations?

Feature

Explanation

Version Control

Track schema changes with version numbers.

Rollbacks

Revert unwanted changes easily.

Deployment

Sync production DBs with codebase changes safely.


Installing Doctrine ORM and Migrations

Doctrine installation setup link


Create Entity Example

Create User.php in (module\Application\src\Entity\User.php)

namespace Application\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="user")
 */
class User
{
  /** @ORM\Id @ORM\GeneratedValue @ORM\Column(type="integer") */
  private $id;

  /** @ORM\Column(type="string", length=255) */
  private $email;

  /** @ORM\Column(type="string", length=255) */
  private $password;
}

Generate Migration

php vendor/bin/doctrine-migrations diff


This creates a file like:

public function up(Schema $schema): void
{
  $this->addSql('CREATE TABLE user (id INT AUTO_INCREMENT NOT NULL, email VARCHAR(255), password VARCHAR(255), PRIMARY KEY(id))');
}

Run Migration

php vendor/bin/doctrine-migrations migrate

Rollback

To rollback the previous change:

php vendor\bin\doctrine-migrations migrate prev