Symfony2 Generate Doctrine Entities from .yml files - symfony

So I have an existing database but I was unable to follow the steps outlined here: http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html because some of my tables have foreign keys for primary keys.
My solution has been to create a copy of the database without foreign key constraints and generate the .yaml files from THAT first.
So now I have XXX.orm.yml files in ./src/My/MainBundle/Resources/config/doctrine/metadata/orm
Next I tried to turn these .yml files into Entity classes with annotations by using this command:
php app/console doctrine:mapping:import MyMainBundle annotation
However that ignores my .yml files. It either generates the generic classes from my database without foreign keys, or it throws an error if I use it on my real database. It never even looks at my .yml files.
So all I want to know, is how can I transform the *.orm.yml files to Entities?

I'm not 100% sure if this is all I had to do to fix it, but I think the solution was as simple as moving my .orm.yml files from
./src/My/MainBundle/Resources/config/doctrine/metadata/orm
to
./src/My/MainBundle/Resources/config/doctrine
and running
php app/console doctrine:mapping:import MyMainBundle annotation --path="./src"

Use convert after import to convert yaml to entity annotations :
php bin/console doctrine:mapping:convert annotation src
See --help for further informations.
To force override entity files use --force option.
To create accessors (getters and setters) use
php bin/console doctrine:generate:entities yourBundle
Don't forget to check if yml files don't override behavior of annotation changes...
Regards

--path isn't an option to the command doctrine:mapping:import

after:
php bin/console doctrine:mapping:import YourBundle yml
use:
php bin/console doctrine:generate:entities YourBundle

Related

Generate orm.yml mapping file from existing entity file in Symfony

Is there a way to generate src\AppBundle\Resources\Config\doctrine\example.orm.yml file from an existing entity file src\AppBundle\Entity\Example.php, rather than use php bin/console doctrine:generate:entity to create a new entity?
To Generate this file, you need something that doctrine can use to do it.
So if you have your database already up, you can do a
php bin/console doctrine:mapping:import
but if you want to do it "code first", i guess you need to create the mapping information yourself and then use
app/console generate:doctrine:entities <Your Bundle>
app/console doctrine:schema:update --dump-sql
app/console doctrine:schema:update --force

In Symfony, what's the purpose of src/AppBundle/Resources/config/doctrine/*.orm.yml files?

I wanted to create an entity, basing on a database table. I followed these steps: Doctrine/Symfony entity generator and generating entity from one table , that is, in terminal:
php bin/console doctrine:mapping:import --force AppBundle yml --force --filter="Client"
php bin/console doctrine:mapping:convert annotation ./src/AppBundle/Entity --from-database --filter="Client"
php bin/console doctrine:generate:entities AppBundle:Client --no-backup
This created the entity "Client". But, it also created a client.orm.yml file in the src/AppBundle/Resources/config/doctrine directory. And this causes problems - that is, my application now crashes with an error message:
"No mapping file found named 'User.orm.yml' for class
'AppBundle\Entity\User'"
I think Symfony/Doctrine started using those yml files to map my php code to database schema. But why? I didn't want it. I just wanted Doctrine to create an entity.
p.s. When I remove this client.orm.yml file, everything works fine. But I don't understand what is happening, and how should I handle it properly.
You can delete these files after entity creation process. These files for metadata mapping.
You can read this
martinfowler.com/eaaCatalog/metadataMapping.html and martinfowler.com/eaaCatalog/dataMapper.html
Why doctrine does not delete ? Because, it is first step for convert process. Your first command creating those files after you command to doctrine and it is creating Entity files referenced with those files. :)

Symfony2 - Doctrine - Old tables beeing created on schema:update

I am using symfony2 and doctrine and today using command
php app/console doctrine:schema:update --force
encountered the following error:
[Doctrine\Common\Persistence\Mapping\MappingException] Class
'AppBundle\Entity\Linkksiazkaokladka' does not exist
eventhough I had no Entity\Linkksiazkaokladka.php file.
I have also noticed that:
php app/console doctrine:generate:entities AppBundle
generates entities which are not available in /entity/ directory.
Make sure that your Resource/Config/Doctrine directory is clear.
It seems that Doctrine generates entities first of all based on data in this directory.
In my case clearing Resource/Config/Doctrine helped.

Setup doctrine command to generate only mapping file

I'm new to symfony and doctrine, so please correct me if I'm wrong.
If I run the command
php app/console doctrine:generate:entity
Doctrine creates an Entity with the given class name and the fields with the given types. Also it creates the mapping file.
Now what I would like is a command which creates only the mapping file.
Something like:
php app/console doctrine:generate:mapping
where doctrine creates only the mapping file on the class I wrote. Is that possible, how can I achieve that?
I don't understand fully what you need to do, in any case mapping files I guess could be generated only if read from an existent database or with a conversion of the mappings:
(in symfony) $ php app/console doctrine:mapping:convert
If you would like to use doctrine command it is in:
$ php vendor/doctrine/orm/bin/doctrine
But I never run this in a full stack symfony framework.

Symfony Generating Specific Entities from the Database

I have a larger database and I'd like to create entities for 4 tables. I'v been referencing this Symfony Cookbook but it's not help quite getting me where I want to be.
Step 1 - Import From Database
php app/console doctrine:mapping:import --force AcmeBlogBundle xml
Question Here: Can you import specific tables? or all tables that start with 'Abc%'?) What command would do this?
Step 2 - Convert the import to Annotaction|Yml|XML
php app/console doctrine:mapping:convert annotation ./src
Problem here: Even if I delete the imported files and leave the specific orm.xml I'm interested in, they still seem to affect this command. Is there a cache to clear or something??
Step 3 -- Create the Entities
php app/console doctrine:generate:entities AcmeBlogBundle
I haven't had any problems here.. However, getting to this point requires creating and deleting a lot of unnecessary files which is a real headache from a version control perspective.
I think the steps should be done in below arrange and you need to use --filter for the specific table:
Step1
php app/console doctrine:mapping:convert annotation /src/App/MyBundle/Resources/config/doctrine --from-database --filter="table_name"
Step2 Now you can apply importing
php app/console doctrine:mapping:import AppMyBundle annotation --filter="table_name"
Step3
php app/console doctrine:generate:entities AppMyBundle --no-backup

Resources