Sam
Sam

Reputation: 1828

Doctrine2 what is the best way to update entities? ( Database => Doctrine )

Let's say I have all my entities set and I have my repositories linked in annotations like this:

/**
* User
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
* @ORM\Table(name="user")
*/
class User
{....}

now I go to my sql database and add a new column or any other change. I need to update doctrine model and at the moment I do it like this :

1 ) php bin/console doctrine:mapping:import App\\Entity annotation --path=src/Entity

2) php bin/console make:entity --regenerate App

these commands are purging my entities annotations and I get:

/**
* User
* @ORM\Entity
* @ORM\Table(name="user")
*/
class User
{....}

3) I have to relink all my repositories manually in each entities (I have 12)

QUESTION: Is there a way to update from database to entity while keeping all repository links at the top of my entities? How do you do it ?

I could not find anything in the doctrine manual; I was hoping for a doctrine:mapping:import setting ?

Upvotes: 2

Views: 2037

Answers (1)

Denis Alimov
Denis Alimov

Reputation: 2891

Instead of going from database to entity, go by another way: from entity to database

  1. add needed property to the entity. like
/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=255, nullable=true)
 */
protected $name;
  1. run php bin/console doctrine:migrations:diff you will get migration with changes like $this->addSql('ALTER TABLE user ADD name VARCHAR(255) DEFAULT NULL');

  2. run migration


EDIT

If you still want to go by your way, at least you could narrow down changes only to one entity by adding --filter="user" to both your commands

Upvotes: 3

Related Questions