Advertisement
Google Ad Slot: content-top
Zend Alter Table
What is ALTER TABLE in Doctrine Migrations?
In Doctrine, ALTER TABLE refers to modifying an existing table schema using a new migration file. Common use cases:
- Add or remove columns
- Change column types or constraints
- Rename columns
Example Use Case: Add a status boolean column to the user table
Modify the Entity
Update 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;
/**
* @ORM\Column(type="boolean", options={"default" : false})
*/
private $status;
}
Generate Migration
Run this in terminal:
php vendor/bin/doctrine-migrations diff
This creates a file like:
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE user ADD status TINYINT(1) DEFAULT 0 NOT NULL');
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE user DROP status');
}
Run Migration
php vendor/bin/doctrine-migrations migrate
Your user table now has a status column of type boolean.
Change Column Type
you have a user table with a column status that is currently boolean , and you want to change it to string.
Before:
/**
* @ORM\Column(type="boolean", options={"default" : false})
*/
private $status;
After:
/** * @ORM\Column(type="string", length=10) */ private $status;
Generate Migration
Run this in terminal:
php vendor/bin/doctrine-migrations diff
Run Migration
php vendor/bin/doctrine-migrations migrate