Create database from Doctrine model with Symfony - php

I've created a new database with the SQL CREATE DATABASE command to host my application tables. I would now generate the database from my Symony 2.1 project using Doctrine. I already have the correct mapping YML-PHP entities but when I try to use the command
php app/vendors doctrine:schema:create
it fails on a query that I run inside my application. What I don't understand is why it seems that it's trying to boot my bundle and so obviously it fails because the queries that I execute don't find the tables. How can I generate the new database?
Base table or view not found: 1146 Table 'mydatabase.mytable_menu' doesn't exist

That command only creates the database for you. You will need to run this command to generate the actual database tables for your entities.
php app/console doctrine:schema:update --force

Related

Doctrine generate/migrate command doesn't make tables

I am learning symfony and wanted to make a login page. So I followed this tutorial -Symfony 4 login form with security and database users exactly the same.
The issue I am having is the doctrine:generate and doctrine:migrate command.
When I run the command, it does not make the tables. When I check the database with phpmyadmin, all I see is a migration_versions table and no tables for the entities I created.
The doctrine:migrations:generate command will create an empty migration file. You still have to write the sql necessary to create your tables.
The doctrine:migrations:diff command will check the database structure against your model classes and generate the sql for you.

how to connect to another database in symfony other than the configured one

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>

How to update Symfony Entities from an existing database?

I found in Symfony docs that you can Generate Entities from an Existing Database so are there any ways to update entities from existing database I added using MySQL?
For example, I added new column to my FOSUSERBUNDLE database called "batman"
using this command on MySQL:
ALTER TABLE cs_symfony_members
ADD batman int(11) NOT NULL;
how to update my entities i generated using the docs guide above ?
If you need to update database schema to Entity, you can use two command:
(1) Create entity mapping from database:
bin/console doctrine:mapping:import BundleName --filter=EntityName
(2) Update your entity to match this mapping:
bin/console doctrine:generate:entities BundleName:EntityName
Example, I have a User table in database, a had changed structure of this table. And now, I want to update these changes to my User entity in AppBundle. I need to run following command:
bin/console doctrine:mapping:import AppBundle --filter=User
bin/console doctrine:generate:entities AppBundle:User
I hope this can help you!
For anybody who is having trouble finding this command after forgetting, like me, here it is:
When i update my Entities from my already existing sql database, I juste use this command:
php bin/console doctrine:mapping:import --force AppBundle annotation --filter="Entity name".
I'm using symfony 3.4 but I'm gussing this also works for later versions.

How to remove an entity class in a Symfony2 project

When we initially designed our project, we had a couple of entities that to date are unused (and we don't plan to implement them in the near future). Ergo I want to remove them from my project. I would proceed like this (all steps manually performed):
Remove all relations from my currently used entities.
Delete the doctrime ORM file src/Resources/config/doctrine
Delete the class PHP file from src/Entity
Remove the table from the database
What I would like to know: Are there any routines (e.g. console commands) that may support this procedure? For example, if I run
php app/console doctrine:schema:update --dump-sql
after having removed all relations and deleted the files, that I get the SQL statement that removes the according table(s)?
Once you have removed the entities from your code, you can use the following console command to drop the tables:
php bin/console doctrine:schema:update --complete --dump-sql
Note the use of the --complete option.
Here are the relevant parts from the doctrine:schema:update help text:
Options:
--complete If defined, all assets of the database which are not relevant to the current metadata will be dropped.
[...]
Help:
[...]
Finally, be aware that if the --complete option is passed, this task will drop all database assets (e.g. tables, etc) that are not described by the current metadata. In other words, without this option, this task leaves untouched any "extra" tables that exist in the database, but which aren't described by any metadata.
Hint: If you have a database with tables that should not be managed by the ORM, you can use a DBAL functionality to filter the tables and sequences down on a global level:
$config->setFilterSchemaAssetsExpression($regexp);
to remove the table in symfony 3 you can just run a migration and the table not in use will be dropped from the DB:
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
Your steps to delete an entity are OK.
You can't remove a table from Doctrine, as Doctrine doesn't know about it. Have a look at this question:
Deleting table using Doctrine2 and Symfony2

Is there a "generate-migrations-db" equivalent in Symfony 2 / Doctrine 2?

Here is the legacy documentation explaining what "generate-migrations-db" does:
http://symfony.com/legacy/doc/doctrine/1_2/en/07-Migrations
It says
Generate migration classes from existing database connections
(doctrine-generate-migrations-db, doctrine-gen-migrations-from-db)
Also:
Generating Migrations
Doctrine offers the ability to generate sets of
migration classes for existing databases or existing models as well as
generating blank migration classes for you to fill in with the code to
make your schema changes.
From Database
If you have an existing database you can build a set of migration
classes that will re-create your database by running the following
command.
$ ./symfony doctrine:generate-migrations-db
In other words: it takes the schema from the database and generates a migration that performs that schema creation. No entities, no classes, no mappings are used in this process. It just takes a DB and builds a migration class.
We do not have generate-migrations-db anymore. Do we have something that performs that task? I couldn't find. If it was replaced by some other command, please let me know. If it was just removed, please let know.
I'm not aware of a command in Doctrine or the Migrations Bundle that creates migration files for an existing database.
So here's how I did it instead:
Install DoctrineMigrationsBundle
Create a new blank database
Update your config or parameters to point to this blank database rather than to your "real" one
Run php app/console doctrine:migrations:diff. This will create a migrations file that creates your database tables etc from scratch
Change back your config/parameters
Hope this is helpful.
Take a look at the DoctrineMigrationsBundle, which can generate migration classes with sql statements for migration.

Categories