My goal is to get access to multiple databases in One Project Bundle.
I read through the symfony2 docs and managed to do the followings:
configure multiple connections for different Bundles
generate Entities from ONE Existing Database using:
php app/console doctrine:mapping:import AcmeBlogBundle annotation
php app/console doctrine:generate:entities AcmeBlogBundle
But I cannot find ways to generate Entities from MULTIPLE Existing Databases in SAME Bundle so that I can access multiple databases in One Bundle. Any Ideas?
P.S. I am not familiar with Doctrine. So actually if there are ways to do Symfony2 without Doctrine, I would also appreciate.
UPDATE #1:
Cerad's answer comes quite close. Yet one problem is not yet solved. As I have some same table names in different databases, it's better to organised them into separte folders inside Entity Folder. I have checked similar posts like this and that. But the solutions are not working for me. Their solution simply puts all entities directly into Entity Folder, ignoring the specified dir option in config.yml. Are there workarounds for this problem?
The first step it to configure multiple entity managers (not connections), one for each database. You then use the --em option on the doctrine commands to specify which entity manager to use.
php app/console doctrine:mapping:import "AcmeBlogBundle" annotation --em=name1
php app/console doctrine:mapping:import "AcmeBlogBundle" annotation --em=name2
Be aware that you not going to be able to directly query (join) across multiple database with doctrine. At least not very easily. As long as you plan on limiting your queries to one database at a time then you will be fine.
This is actually a somewhat advanced topic. You might want to spend some time with the doctrine documentation. Might also be easier to get started with one database and then split later.
Related
I want to use an existing MariaDB with a new Symfony 6 project and Doctrine.
Unfortunately, I don't understand how to create the entities and repository from an existing database. The database is very complex and has a lot of relations. Is there an importer for this?
The following command seems to be deprecated. It works, but only creates the entities (but probably not completely correct).
symfony console doctrine:mapping:import "App\Entity" annotation --path=src/Entity
Thanks for your help.
You can use this kind of command :
symfony console doctrine:mapping:import "App\Entity" annotation --path=src/Entity
But be aware that this solution is deprecated.
What Symfony's team recommend is to create you entities yourself using the maker bundle.
Keep in mind that the maker only generate PHP classes, so you shall be able to reproduce your entities this way.
It's gonna take you some time, but it's the best way to do actually.
Just be careful with the naming of your relations (especially with the many to many pivot tables) wich may not be as generic as what the maker bundle generate
I want to connect to another database and/or create a new one in symfony. How can I do that and at the same time get access to the tables of it?
I looked for how to do this inside the app
php bin/console doctrine:query:sql 'SELECT * FROM product'
but couldn't find any answer.
You can't create a database directly from Symfony, but you can use Migrations to create tables.
If you want to you can even generate the Migrations using the bin/console and doctrine:migrations:diff to generate migrations based off your Entities.
For the multiple databases you can use Service Containers to configure the Doctrine ORM then in your code you can grab the specific container and instantiate doctrine from there.
Also if you are interested in doing selects I would take a look at DQL as this allows solutions as follows:
// This assumes an entity called 'entity' exists.
$query = $entityManager->createQueryBuilder('entity');
return $query->getResult();
Look at this, should help: https://symfony.com/doc/current/doctrine/multiple_entity_managers.html
I short, you can define different entity managers that deals with different database connections
You need to configure multiple entity managers, check How to Work with multiple Entity Managers and Connections.
Then you can use doctrine commands as below:
php bin/console doctrine:database:create --connection=<your_entity_manager_name>
php bin/console doctrine:query:sql --connection=<your_entity_manager_name>
I have already a few entities, but now a new table appeared in a database, and I'd like to generate an entity on only this one table.
I already saw this, but I have further questions.
I already have a User entity (and a db table). Now, the new table is called "Report" (no entity for it right now, I want to create it) and it has a foreign key to User. There are also a few more foreign keys.
If I do what is suggested in the above answer, that is:
$ php app/console doctrine:mapping:import --force AppBundle xml --filter="Report"
$ php app/console doctrine:mapping:convert annotation ./src/AppBundle/Entity --from-database --filter="Report"
$ php app/console doctrine:generate:entities AppBundle:Report --no-backup
Will Doctrine generator try to modify my User entity? Or will just create a Report entity?
Btw. I understand(?), this it will not, because this is ManyToOne relation, but let's assume for a moment that this is ManyToMany for a moment.
I know I could simply try it, but the last time I executed the doctrine:mapping:import --force command I ended up with a crashing app and I spent many hours to solve this problem, until someone on Stackoverflow told me to remove the src\AppBundle/Resources/config/doctrine/ directory, which helped.
I didn't think about making a backup before running this command.
So, I'm a bit afraid of it... Now I created a backup, but I'm not sure if this will help in case of troubles.
It depends on the /Resources/config/doctrine directory. If you empty it out and do the commands you described, then only the Report entity will be modified. If you have all of the previous mappings in that directory it should modify the User entity as well.
The commands will automatically create backups of your entities for you, so you will have a new User.php and a backup in User.php~, which you should be careful of if you are using version control and like to git add src/ without thinking too hard about it. You don't want to add those files to source control.
Either way, backups are created, and you should be using version control on top of that, so you should be fine.
I am in the process of upgrading from Doctrine 1.1.4 to Doctrine 2.0.6 in my Zend application. I have installed the Doctrine 2 command line tool.
In Doctrine 1.1.4, I generated the model classes directly from the database (using Doctrine::generateModelsFromDb()), is this possible in Doctrine 2, or do I have to go through the 'mapping' process i.e. by providing Docblock Annotations, XML or YAML structures of the tables.
The reason I ask this is because there is a 'setAutoGenerateProxyClass' option in Doctrine 2, I got the impression that this means it will generate the proxy classes from scratch.
Appreciate the help.
Autogenerate proxyclasses means basically that Doctrine 2 will automatically generate "proxy classes" for your entities, instead of only generating them manually using generate-proxies. Proxies are used when you have relations in your entities and they need to be lazy-loaded.
To generate mapping information from the database, you can use convert-mapping:
php doctrine orm:convert-mapping --from-database yml /path/to/mapping-path-converted-to-yml
Bear in mind that this is only recommended to be used as a starting point. The database driver is not able to correctly generate mappings for all possible combinations of options, so you probably should just run this once and then write mappings yourself.
See Doctrine 2 manual, "Reverse Engineering the database"
You can use the "annotation" as driver, if you want to get the generated entities:
php doctrine orm:convert-mapping --from-database annotation generatedModels
I am new to ORM and I am really keen to learn it. I successfully managed to install all classes and configurations for Doctrine 2.1 with Zend 1.11.x by following this tutorial.
http://www.zendcasts.com/unit-testing-doctrine-2-entities/2011/02/ Which uses Bisna plugin and doctrine scripts.
Now my problem is he is clearly explaining how to create entities and tables through doctrine classes but do not explain how to auto generate the proxies and repo classes from already existing database which helps me to select, insert and update. I always create my databases using MySQL Workbench.
I also followed the below tutorial as well
http://www.zend.com/en/webinar/Framework/70170000000bSrG-webinar-zf-v-1-doctrine-v-2-20101214.flv
My database is so complex with relationship flowing across every possible way. If I follow the steps which is explained in these tutorials I will never complete my project. Can any one please explain how to start using Doctrine after configuration. Considering I already have a database and my Model folders are empty. I have my folder sructure as below.
C:/zf/library/Doctrine
C:/zf/library/Symfony
C:/zf/library/ZC -- (my model which should contain the proxies and repo of Doctrine. At the moment it contains nothing.)
C:/zf/library/Zend
C:/zf/scripts/doctrine.php
Please help me!
I posted this same post yesterday and no one replied to my post. Please let me know if you need anymore information from me.
Thank you,
Karthik
According to Doctrine you should create your entities first yourself and then create your database schema from these entities.
But because you already have a database you probably don't want that. It is possible to convert your database to Doctrine2 entities in PHP, XML or Yaml.
You should take a closer look at the commandline tools Doctrine offers with the Bisna glue because there you can generate a lot of stuff.
To generate your entities FROM your database consider the following command:
php doctrine.php orm:convert-mapping --from-database php ../library/Application/Entity
You can also define the namespace and a base class which your entities have to extends with: --namespace=namespace and --extends=class.
Doctrine2 warns you to convert your database to entities because not everything could be auto detected or supported. For instance ENUM datatypes are not supported by default in Doctrine2 so converting your database will raise an error.
It's a good idea to check all your entities especially associations before you use them. Hope it helps you.
If I understand your question correctly, you have your entities already configured and need to auto-generate your proxy and repository classes.
Both can be created using the following Doctrine CLI commands from your application's root directory:
php scripts/doctrine.php orm:generate-proxies
php scripts/doctrine.php orm:generate-repositories library/
If you're looking for a way to auto-generate your entity classes, unfortunately I don't think a solution is available for this yet.
A support rep at ORM Designer said they are "working on" this feature and that it is "very demanded." Here's hoping it will be included in ORM Designer 2.0 since there is generally a lot of repetitive work involved in coding/mapping entity classes that could likely be automated.
You can use the orm:generate-entities command if you provide mapping information in either XML or YAML format.
See http://www.doctrine-project.org/docs/orm/2.1/en/reference/tools.html#entity-generation
For development, set proxy generation to be automatic in your config, otherwise, use the orm:generate-proxies command.
Unless you need to customise your repositories, generic ones are created in the entity manager when requested. To specify custom repositories, simply use the repository-class entity mapping attribute.
See http://www.doctrine-project.org/docs/orm/2.1/en/reference/xml-mapping.html#defining-an-entity for an example