How to use an existing Database with Symfony 6 and Doctrine - php

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

Related

Use Symfony entities and doctrine mapping without database

We are migrating our project from custom CMS to Symfony 5.1. Instead of Database, we are using REST API. Currently we are using Entities without connecting them to any database (with no #ORM annotation or configuration file), and we put all REST logic inside a Repository folder
Currently, if we save our entity in SESSION
$example = new Example();
$request->getSession()->set('example', $example);
On every next request, we will get error:
Class "App\Entity\Example" is not a valid entity or mapped super class.
Of course, this could be fixed if we put correct #ORM annotations, but then it requires a database.
Can this be fixed, and if it's not possible, what would be proper architecture for this scenario?
Let doctrine be the tool to manage the data. In fact that there is no database, you will need something similar to let doctrine manage the data of the REST API.
Take a look at this bundle:
https://github.com/CircleOfNice/DoctrineRestDriver
Edit:
In addition to one of the comments, the DoctrineRestDriver is not maintained anymore. In fact, the bundle does not support Symfony 5.x.
To use the functionality of DoctrineRestDriver you could downgrade your symfony to the supported 4.x.
Your architectur to use doctrine to manage your rest-api can be solved by using the DoctrineRestDriver package.
If you don’t want to downgrade your symfony, you will have to implement your own „data-manager-solution“, to use the api as your database.
But I think the architectural problem with the rest api doesn’t relate directly to your current error. We need more code to understand, where your specific mapped super class error is from (repository, entity).

Symfony2: How to generate Entities from MULTIPLE Existing Databases in SAME Bundle?

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.

Is it possible to generate model classes using Doctrine 2 directly from the database?

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

Zend 1.11 and Doctrine 2 Auto generate everything needed from already existing database

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

Codeigniter with Doctrine for large application

I am about to start a project on PHP and selected Codeigniter as a framework to be used after receiving lot of plus comments from Codeigniter-users ;-)
I am not much clear about which ORM to be used with codeigniter. I was advised to use Doctrine. Is there any tool available for codeigniter to create models and db mapping with Doctrine on command line?
You can use Doctrine itself for creating models. Follow the cookbook
http://www.doctrine-project.org/projects/orm/1.2/docs/cookbook/code-igniter-and-doctrine/en
to setup, so then in /application/ you can use
$ ./doctrine [command]
to create models and mappings.
Note: in this moment I'm failing to access any page at the doctrine-project.org site. May be later...

Categories