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 !
Related
I have a database table which has a lot of fields. I want to create an Entity which maps only to few of those fields.
Is there a practical way to achieve that?
When I try to make an entity and mapping it via annotations, the doctrine:schema:validate command says that the entity is not in sync, and is right.
When I try to make a doctrine:schema:update it automatically drops all the fields that the entity doesn't have. I want that the schema update command updates only the fields written in my entity class.
With Doctrine ORM you map your database table to a PHP class and a row from that table is mapped to an instance of the entity class.
So i am pretty sure that you have to map all your fields. Otherwise if you dont want to - user ActiveRecord, it is possible there.
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.
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.
I am using Doctrine2 with codeigniter. my project has created but i have to add prefix in database table. In codeigniter I have solved this issue with dbprefix $db['default']['dbprefix'].
But Doctrine not support it because Entity classes are not created with prefix. So it can't find Table.
I want to add table prefix in doctrine also without change in Entity class name of doctrine. Is there any possibility to add some prefix.
I have search TablePrefix Class but this class not working in my Doctrine library.
Previously Project created without prefix table name like "user" and entity annotation has also created with "User". But now I added prefix in all table "my_user"
Please Help!!
You can add a table prefix by using Doctrines event manager as described in the Doctrine documentation here:
http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/cookbook/sql-table-prefixes.html
In a previous question, simshaun has given a good example of how to implement this in symphony:
How to setup table prefix in symfony2
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