Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better use of models and migration in symfony

Hey. I'm having a hard time migrating changes I've done i my config/doctrine/schema.yml file.

I added the column age to the user table. Then I did a php symfony doctrine:generate-migrations-diff followed by php symfony doctrine:migrate .

Looking in my database, the column age is now added, without deleting any data.

But, my /lib/model/doctrine/base/BaseUser.class.php is not changed, there is no age field or functions for age . So I also did the command php symfony doctrine:build-model . Finally the model is updated/migrated too.

So I wonder, is this the only way? Seems like a lot of work, and I'm afraid to miss something each time doing it.

Could I go right into phpmyadmin, add changes in the database there and just do a php symfony doctrine:build-schema , and like that skip the migration part (two commands).

Also when the comes to use of models, am I right that /lib/model/doctrine/User.class.php is where I can make functions and such for my User "data class"? Like, making a function isFemale . If not, where would that kind of function be?

This might be a bad question, but why is the model layer inside the /lib/doctrine path? As far as I have learned, you keep modules inside apps, where you create your view and controller. Why should the model be outside. Like this I can make models without attached controller and view?

Thanks.

like image 777
Johannes Avatar asked Jan 25 '11 23:01

Johannes


1 Answers

Why should the model be outside

Because models can be used everywhere in your project, in example, in different applications and modules.

Could I go right into phpmyadmin, add changes in the database there and just do a php symfony doctrine:build-schema , and like that skip the migration part (two commands).

Of course you can, but migrations are a good approach to track your schema when deploying to production or working in team.

Here how I use doctrine migrations (simple use-case):

  1. Add a column age to my User model in schema.yml
  2. ./symfony doctrine:generate-migrations-diff. Migration class(-es) have been generated.
  3. ./symfony doctrine:migrate. Column age successfully added to table.
  4. ./symfony doctrine:build --all-classes. Build forms/filters/models

That's it. The main idea is that doctrine:generate-migrations-diff class:

  1. Gathers information about all your models' structure (php-representation of schema.yml)
  2. Compares your schema.yml and info from (1)
  3. Generates migration classes based on difference

Also when the comes to use of models, am I right that /lib/model/doctrine/User.class.php is where I can make functions and such for my User "data class"? Like, making a function isFemale . If not, where would that kind of function be?

Yes, you can add such method to User model because it's about users.

like image 76
Darmen Amanbayev Avatar answered Sep 24 '22 23:09

Darmen Amanbayev