I have a very big problem with symfony module generation as i always make modification in my database (add tables , add columns, changes names, changes columns data-type .....) and i want a good way to build the symfony module without any effect on the modules that i didn't change it and without drop the data in the database... on brief i need to update my symfony module with my changes ONLY.
I think you mean "models" instead of "modules" here.
If this is the case, basically once you've got data in the database that you don't want to drop:
1 - Make any needed changes to your schema.yml
2 - Use MySQL ALTER TABLE etc to make changes directly to your database
3 - Use the following commands to regenerate your models:
symfony doctrine:build --model
symfony doctrine:build --sql
symfony doctrine:build --forms
symfony doctrine:clean-model-files // to clean up old stuff
Assuming you haven't made changes to your base classes (which you shouldn't do), the rebuilding of the models shouldn't break anything. You're basically re-building everything but only the changes you introduced in your schema will be introduced.
Related
I'm currently given a running website, which using Zend-Framework 1.11 (to be precise, 1.11.11) with Doctrine 2.3 on it.
[I know, it's a very-very OLD version, but unfortunately, I can't upgrade it to newer version, since I'm really afraid that it will causing more trouble and destroy this website entirely lol!!]
I'm new with this ZF, but having a bit experience with other framework, such as - Laravel and CodeIgniter.
So, my question is -
How can I add a new table (or column) to an existing database (or table) using ZF-way or CLI-way?
For example - I'm having a Table called Supplier, with 4 columns
id
name
contact
phone
I want to add mobile_phone, a new column. How can I do that?
I need to add a new Table, exactly the same like Supplier above, how can I do that?
I experienced before, to add 2 new tables, BUT I did that manually... and it's such a pain.
Because, I need to create a file, edit the existing Model file (to create a relationships/FK) and then edit the Model Entity, and surprisingly, when I try to create its functionality, like Insert/Update/Delete, it shows me an error, that I need to create a Model Proxy too, which I check, it suppose be auto-generated.
Is there any good way to add that table (or column)? maybe using CLI or else?
Is there a way, to check, IF I can use doctrine-CLI? maybe something like doctrine --v? or something like that
I did try php doctrine list on project root folder and it fails - Could not open input file: doctrine.
I found those questions (maybe) same like here - Add a new column in Doctrine 2 in Zend Framework.
But, to be honest, I really don't know how it works and is that can be executed on project root folder?
I did try check on the details link about doctrine CLI that provided there, but still, I don't understand how to set it up.
On that link too, it drives me to this link, about how to set-up doctrine CLI, but, as I state, I kind of confuse, how I add / create the file.
In summary, I want to know, is there any way, to create a new Table? Like, on Laravel, we can create a new Table using their artisan:make?
And, is there a better way, to add new a Column on the existing Table?
FYI - I can do this syntax on my project root folder zf show version.?, IF, I can use zf syntax to create table and adding new column, I'm okay with that too. :)
Thanks in advance & Cheers! :)
I'm really sorry, if this thread/question is too long or not a straight-forward question, as I'm getting frustrated, to find a good documentation about this old ZF version
I'm using the symfony plugin "sfMultipleAjaxUploadGalleryPlugin", and I would like to modify it's model.
I want to change the "Photo" class, removing the field "title" and adding the fields "description_fr" and "description_en".
I've made the changes in phpmyadmin, and, in the plugin, I've edited the schema, and the models, filters, and forms to reflect the changes.
I've emptied the cache, but I get doctrine errors concerning the new fields. This plugin is like the admin generator, it extends the class "autosomething", created in the cache at runtime.
Any ideas ? Where else could the photos' model be defined ?
Since it is in the plugins/ folder, I can't just recreate everything using the command-line client.
I am afraid the changes in phpMyAdmin are completely useless, since once you will do a build everything in the database will be managed by symfony itself.
You should first make the wanted changes in the schema, then adjust the relevant classes
in the model
sf_plugin_dir/sfMultipleAjaxUploadGalleryPlugin/lib/model/doctrine/Photo.class.php and sf_plugin_dir/sfMultipleAjaxUploadGalleryPlugin/lib/model/doctrine/PhotoTable.class.php,
in the form sf_plugin_dir/sfMultipleAjaxUploadGalleryPlugin/lib/form/doctrine/PhotoForm.class.php
and possibly in the filters sf_plugin_dir/sfMultipleAjaxUploadGalleryPlugin/lib/filter/doctrine/PhotoFormFilter.class.php
and check also possible helpers
in sf_plugin_dir/sfMultipleAjaxUploadGalleryPlugin/lib/helper/.
Then you should check every action/template using the changed Photo model (if any) and adjust accordingly.
Finally empty the cache
symfony cc
be sure the changes are reflected in all the model classes (not really important, but still)
symfony doctrine:clean
rebuild everything
symfony doctrine:build --all --no-confirmation
it should work now.
What is the best way to rebuild a model without loss data in MySQL for Symfony?
What if I have a big site, and a lot of data in the database and I would like after six months to add few new fields to database?
You can use migration.
Doctine manual
Symfony task for migrations
Slideshare presentation
Slideshare presentation
So you need write migrations, migrate, and build your models, forms, etc.
I suggest you use #denys281 for Symfony1.4 ....in Symfony2 however its VERY simple ... just use the command :
php app/console doctrine:schema:update --force
It compares what your database should look like (based on the mapping information of your entities) with how it actually looks, and generates the SQL statements needed to update the database to where it should be. In other words, if you add a new property with mapping metadata to Product and run this task again, it will generate the "alter table" statement needed to add that new column to the existing product table. So it doesnt remove any data
There is also a DoctrineMigrations bundle for Symfony2 if you fancy that route -> http://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html
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.
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):
Add a column age to my User model in schema.yml
./symfony doctrine:generate-migrations-diff. Migration class(-es) have been generated.
./symfony doctrine:migrate. Column age successfully added to table.
./symfony doctrine:build --all-classes. Build forms/filters/models
That's it. The main idea is that doctrine:generate-migrations-diff class:
Gathers information about all your models' structure (php-representation of schema.yml)
Compares your schema.yml and info from (1)
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.
So heres the scenario:
Currently we have a development site with 3 models. We found we didn't like our initial schema and added a few rows. We re-generated the schema (doctrine:build-sql).
Now it forced us to drop and re-create all the tables and dump back in all the information as no ALTERS were created but rather CREATE statements only. Not a problem...
The big problem came to updating the models. After we ran a build-all and such a few errors popped up i.e. "Widget sort not found" etc. We figured out we needed to rebuild the models. So we can a symfony doctrine:build-models course Course (Course was the table name...course the models). This worked great and fixed the broken links within Symfony.
The downside is all custom code in the actions.class.php file was lost as were customizations to the _form.php page.
My question on this is, how do we store our own actions so they are not lost if you update a models schema? Similarly templates and such are re-generated to but do not hold any customizations.
There surely must be a simple solution to updating a model's schema in symfony?
Found my answer to this. You don't update the module per say but the models of the database. You can change your schema.yml file and do a symfony migration
http://www.slideshare.net/denderello/symfony-live-2010-using-doctrine-migrations
If you just want CRUD/minimal customisation, you can do this with the admin generator:
./symfony doctrine:generate-admin appname Course
A regular module can't be updated once generated without losing customisations - they are intended to be a starting point.