I have made a table in a database and that table is connected to symfony2 and when I have already develop the application I have find out that I have to add column to the table called profilePicture for uploading profile pics and I have already generated the entities from the previous version of the table and now my question is
How can I add an entity from the new version of the table ?
The first thing which I advise you to make is to show the effort that you made (show at least some lines of codes).
You have to modify manually your entities and then make both following instructions in the console:
# generate getter and setter
php app/console doctrine:generate:entities YourBundle/Entity/YourClass
php app/console doctrine:schema:update --force
Finally, look at these two link, I think this is very helpful
Official documentation and this question.
Related
I have created my database in Symfony with
php bin/console doctrine:mapping:import "App\Entity" annotation --path=src/Entity
I have created my repository too.
But when I look at the entity directory, one important table is missing.
In this table there is a lot of important information for my website, but I can't access it, because the entity does not exist.
I have no error with the import and not error in the database (I think).
Table on HeidiSQL:
The entity on the Symfony web site:
I have not demande_etape.
Why and how i can make this table in entity ?
I don't no why but if symfony not detect ID key, he can't import the table and make joins in table on both side.
So I created my entity myself and i put id_Demande field and id_Etape field in ID.
It's wotking !
I'm trying to learn the basics of Symfony, and have come across a problem that baffles me.
I'm trying to write Data to a SQL Table, following (mostly) the instructions from the documentation ( http://symfony.com/doc/current/book/doctrine.html ). I created an entity via
$ php bin/console doctrine:generate:entity
and it is mapped to my DB Table correctly, I can read and write, no problem. After that I added a column to my table and the corresponding lines to my Entity:
/**
* #var string
*
* #ORM\Column(name="User_Credentials", type="text")
*/
private $userCredentials;
Then I tried creating the setters and getters, but no changes were made, just a backup of my Entity. So I added getter and setter myself, but when attempting to write to the DB the corresponding column is ommited, remaining empty. No errors are given. I checked and re-checked for typos or other sytax errors, but could not find any. When doing
$ php bin/console doctrine:schema:update --dump-sql
I get
ALTER TABLE st_users DROP User_Credentials;
So it seems my Column is completely ignored. I cleared the cache repatedly, which changed nothing. I am, obviously, lost. Any hint as to the right path will be appreciated.
Thank you.
You can use DoctrineMigrationsBundle for this operations. This bundle will manage your database operations. (Drop, Alter, Create etc.)
Anyway, when you add a new column you can try this: php app/console doctrine:schema:update --force
Well, this is embarassing...
Anyway, if someone else is having the same problem: I had a Tab before the '#ORM\Column' annotation, and it seems the parser can't handle that. Deleting the tab and adding a space did the trick.
Mert's answer is correct anyway, so I will mark it as the right answer.
I added a column to my table and the corresponding lines to my Entity
The concept of Symfony is to add column only to entity, but not to table itself.
Then, when you execute
php bin/console doctrine:schema:update --force
Symfony would detect that you have a new column and it will add column to the table automatically.
PS. Regarding clearing a cache. In case apc cache is enabled in Symfony - you also need to restart Apache or php-fpm process. Otherwise it will ignore new annotations.
I'm trying to auto generate entities from a specific table of my database. I don't want to send doctrine over the whole database, because there are tables in it which are not compatible with doctrine (like tables from the legacy database having no primary keys [yes...]).
Unfortunately, all the blog posts and SO-Anwers doesn't help, as parameter "--filter" isn't working as supposed.
What i've done:
i tried the first step from this SO-Answer and also read this blog post.
php app/console doctrine:mapping:convert metadata_format \
./src/App/UserBundle/Resources/config/doctrine \
--from-database \
--filter="user"
I understood the filter-argument as a solution to filter only for a specific table. But this filter is not being applied. The process gets canceled by an exception "Table XYZ has no primary key" (what is true here and is not part of the question).
I guess i could create a complete new database without the problematic tables for creating the annotations and entities.
But i'm wondering about that what is written about this process and why it does not work.
UPDATE
I also tried out the suggestions from the first answer:
php app/console doctrine:mapping:import --force AcmeBlogBundle yml
AcmeBlogBundle is placeholder for my real bundle. Then the import task stops with:
[Symfony\Component\Debug\Exception\ContextErrorException]
Warning: class_parents(): Class Action does not exist and could not be loaded
The very first table is called "action". When i drop this table, it stops with a simular exception at the next table.
UPDATE 2
The exception mentioned some lines above where caused due to a previous used ORM-Implementation having Classes like "ActionEntity.php" within a Directory called "Entity" for each table. Thats why the exception happened i think. After i dropped this directory, the exception disappeared (but it isn't a good solution in my case).
What I did now: i dropped ALL foreign keys (they made troubles), i setup a completely new symfony project, i run then all these comments from the first answer here and then i got pre-generated entity classes, but without any foreign keys implementation. Those things i have to add now, including several changes to the database scheme because doctrine 2 has some restrictions here like not supporting primary keys as foreign keys and something like that.
But now i can pick those entity-classes and adjust missing connections to other entities.
All in all: this whole process is not really satisfying ...
From documentation:
As the Doctrine tools documentation says, reverse engineering is a
one-time process to get started on a project. Doctrine is able to
convert approximately 70-80% of the necessary mapping information
based on fields, indexes and foreign key constraints. Doctrine can't
discover inverse associations, inheritance types, entities with
foreign keys as primary keys or semantical operations on associations
such as cascade or lifecycle events. Some additional work on the
generated entities will be necessary afterwards to design each to fit
your domain model specificities.
Right steps to import entities from database:
php bin/console doctrine:mapping:import --force AcmeBlogBundle xml
php bin/console doctrine:mapping:convert annotation ./src
php bin/console doctrine:generate:entities AcmeBlogBundle
You can use xml or yml, however it worked only with xml for my case.
I'm using the commands programatically,
This one reads the database indicated in the config.yml file
# Doctrine Configuration
doctrine:
dbal:
default_connection: dbalYmlConfig
connections:
The commands are:-
'command' => 'doctrine:mapping:convert',
'to-type' => 'yml',
'dest-path' => $rawOrmTempSavePath,
'--from-database' => true,
'--force' => true,
This creates x.orm.yml file mapping for each database table schema.
This creates the Entity files from the x.orm.yml's which are in Resources/config/doctrine
'command' => 'doctrine:generate:entities',
'name' => 'MyBundle',
Side note: I put the x.orm.yml files in a temp directory then copy them into the config/doctrine folder adding the bundle name to the x.orm.yml file header, but you could dump the files straight into config/doctrine.
Anyhoo this method works for me.
This might be a dumb question but I'm clueless as to how to go about this.
I've got the entity "MailEntity". However, My database does not contain a table that corresponds with this entity yet.
Question
I would like to know how to generate the table that corresponds with the Entity I created. I've been looking for this but whatever I search on google, the same results seem to pop up constantly.
Update
I've come to know that I can achieve what I want by doing php app/console doctrine:schema:update. Adding the --dump-sql parameter will dump the sql before the schema is actually updated.
However, I would like to do this for a single Bundle. A single Entity would be even better. I just want to create a table from the MailEntity without changing anything else in the database.
If you already have the entity classes ready to go, simply run:
app/console doctrine:schema:update
This should list all the queries doctrine needs to run to create the tables. Run the command with the --force flag to execute them. It's documented here
It might be worth looking at the doctrine migrations bundle, which allows you to diff the current DB status with the entities, and generate migration scripts based on that diff see the docs
You could use php app/console doctrine:schema:update.
This command will tell doctrine to execute the necessary SQL so that your database reflects your doctrine schema. Add --force to actually execute the query.
If you want to check the generated SQL command first, you can use --dump-sql. It will print what would be executed with --force.
Related documentation you may read for further information
EDIT : If you want to create the table for a single entity, the easiest way is to dump the generated sql with --dump-sql and then extract the line responsible of your MailEntity table creation, then execute it manually.
I would not recommend to do it this way though, you should let Doctrine synchronise your database on its own. This kind of tricks may result in errors in your database.
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