Create Entities from existing DB - symfony

Newest Version Symfony2 and using MAMP on a MAC. Following command:
php app/console doctrine:mapping:convert yml ./src/Acme/DemoBundle/Resources/config/doctrine/metadata/orm --from-database --force
My Error:
[Doctrine\DBAL\DBALException]
Unknown database type enum requested, Doctrine\DBAL\Platforms\MySqlPlatform may not support it.
Don't have any idea. What is wrong?
A connection to my db is working. Because I tried to create a table and the output was that the table exist.

Sorry, I missed it and here is the solution of my question.
http://symfony.com/doc/current/cookbook/doctrine/dbal.html#registering-custom-mapping-types-in-the-schematool

Related

generate initial migration after creating entity and repository for an existing database

After creating an Entity and Repository from an existing pre-doctrine database, I am unable to make an initial migration. It gave me this error [ERROR] The version "latest" couldn't be reached, there are no registered migrations. Any idea how to do an initial migration without starting fresh? And for some reason, the migration folder exists outside the src folder, why is it so? In a previous project, the migration folder exists inside the src folder.
Any insight would be appreciated. Thank you for reading.
EDIT: doctrine_migrations.yaml:
doctrine_migrations:
migrations_paths:
# namespace is arbitrary but should be different from App\Migrations
# as migrations classes should NOT be autoloaded
'DoctrineMigrations': '%kernel.project_dir%/migrations'
The commands I used to generate the Entity and its Repository is as follows:
php bin/console doctrine:mapping:import "App\Entity" annotation --path=src/Entity
modified the #ORM\Entity => #ORM\Entity(repositoryClass="App\Repository\UserRepository") in the entity .php
php bin/console make:entity --regenerate
Then when I run bin/console doctrine:migrations:migrate, the error pops up.
Work for me
doctrine_migrations:
migrations_paths:
'App\Migrations': '%kernel.project_dir%/src/Migrations'
and use for migration classes
namespace App\Migrations;
THis is my migrations config.You can test it :
doctrine_migrations:
dir_name: '%kernel.project_dir%/src/Migrations'
# namespace is arbitrary but should be different from App\Migrations
# as migrations classes should NOT be autoloaded
namespace: DoctrineMigrations
For migration commande i use : php bin/console d:m:diff and after this you can use the migration number with this commande :
php bin/console d:m:e --up the_migration_number
I tried most of the methods but it seems that it is possible to generate the migration. However, changes to the entity will not be detected by doctrine.
For example, if I change the field of name to username, php bin/console doctrine:migration:diff does not detect the changes.
What I found worked was exporting the database as .sql, creating the entity the normal way, and manually typing in the fields. Delete the generated table in phpmyadmin, and importing the data back in. Only then would it be working as I want it to be.

Cannot generate Entity from existing Oracle DB

Following symfony's documentation to create entities from exiting database,I get an error when running the first command:
php bin/console doctrine:mapping:import --force AppBundle yml
The error is:
[Symfony\Component\Debug\Exception\ContextErrorException]
Warning: file_put_contents(C:\WebApp\src\AppBundle/Resources/config/doctrine/"user".orm.yml): failed to open stream: Invalid argument
How can I fix it?
Looks like User is a reserved word within Oracle databases, so when I created the table, Oracle created a table named "user" with double quotes. So when trying to generate the entity, doctrine cannot find the table user and found an invalid argument "user" with double quotes in it. The way I solved the problem is by using another name other than user.
Hope this helps someone on the future.
php bin/console doctrine:mapping:import --force AppBundle yml
change to
php bin/console doctrine:mapping:import AppBundle yml --force
more details:
The doctrine:mapping:import command imports mapping information
from an existing database:
php app/console doctrine:mapping:import "MyCustomBundle" xml
You can also optionally specify which entity manager to import from with the
--em option:
php app/console doctrine:mapping:import "MyCustomBundle" xml --em=default
If you don't want to map every entity that can be found in the database, use the
--filter option. It will try to match the targeted mapped entity with the
provided pattern string.
php app/console doctrine:mapping:import "MyCustomBundle" xml --filter=MyMatchedEntity
Use the --force option, if you want to override existing mapping files:
php app/console doctrine:mapping:import "MyCustomBundle" xml --force

ORM convert-mapping is creating the wrong file structure

I currently got orm set up on my silex application. Everything seems to work as expected except for when I run the command to generate the entities from my database (reverse-engineering).
../../../vendor/bin/doctrine orm:convert-mapping --namespace="Random\MyApp\Model\Random\Entities" --force --from-database annotation ./../random/entities
This command will create all of my entities perfectly but they will be under the folder structure
./../random/entities/Random/MyApp/Model/Random/Entities/(files here)
which is wrong since I am expecting to have
./../random/entities/(files here)
A namespace is actually the path under your entities are grouped. So it's perfectly logic that doctrine generates files under this namespace.
If you want a file structure like it just do :
../../../vendor/bin/doctrine orm:convert-mapping --namespace="random/entities" --force --from-database annotation ./
But this is definitely not conform with the PSR-0

Symfony2 doctrine:generate:entity and Fatal Parse Error

I'm getting started with Symfony2 and reading the book I got ot the doctrine entity generator code. Used the example in the book:
php app/console doctrine:generate:entity --no-interaction --entity="AppBundle:Category" --fields="name=string(255)"
and the new entity was created as expected, but I noticed that it generated some PHP code I'm not familiar with:
private $name=string(255);
and
public function setName=string(255)($name=string(255))
I've never seen before the string(255) when declaring a variable nor a function, and when I run
php app/console doctrine:generate:entities AppBundle
It throws an Fatal Parse Error on those lines. Removing the string(255) thing solves it. So, is it fine that Doctrine adds that code and my PHP interpreter's configuration is wrong? Doctrine shouldn't be adding that code or should I remove it after generating the entities? and finally, removing that code won't have consequences in the future?
Thanks,
If you look at the documentation for doctrine:generate:entity, you'll see that the format for declaring the fields looks actually like this:
... --fields="name:string(255)"
So you have to use a : (colon) to separate the field name of the type instead of an equal sign.

Symfony2 generate entities error

I'm trying to generate entities from existing tables in database, I keep getting the same error in a particular entity, which is imported from a second database (or entity manager), but I re-created this table in main database to use the same entity manager and get the same error, so I'm lost about what is happening.
This are my commands to generate it:
php app/console doctrine:mapping:convert yml ./src/MyShop/ProductBundle/Resources/config/doctrine/metadata/orm --from-database --filter="Product" --em=mysecondaryem
(BTW, is there a way to force exact filter? I only need Product)
Which seems to be ok:
Processing entity "ProductSold"
Processing entity "Product"
Exporting "yml" mapping information to...
Then
php app/console doctrine:mapping:import MyShopProductBundle annotation --em=mysecondaryem
Which is weird, as it is logging information about all other tables existing in this db but it's only generating the corresponding to "Product" as filtered (only the files ProductSold.php and Product.php do really exist):
Importing mapping information from "mysecondaryem" entity manager
> writing C:\mysite\src\MyShop\ProductBundle/Entity/ProductSold.php
> writing C:\mysite\src\MyShop\ProductBundle/Entity/Family.php
> writing C:\mysite\src\MyShop\ProductBundle/Entity/Category.php
> writing C:\mysite\src\MyShop\ProductBundle/Entity/Item.php
> writing C:\mysite\src\MyShop\ProductBundle/Entity/Stock.php
> writing C:\mysite\src\MyShop\ProductBundle/Entity/Product.php
Then, in the third step, I get an error:
php app/console doctrine:generate:entities MyShopProductBundle --no-backup
Generating entities for bundle "MyShopProductBundle"
[RuntimeException]
Bundle "MyShopProductBundle" does not contain any mapped entities.
doctrine:generate:entities [--path="..."] [--no-backup] name
As I sayed, I tried omitting secondary entity manager (replicating tables in my first database) and I get the same error.
You've verified that C:\mysite\src\MyShop\ProductBundle/Entity/Product.php has been created, but have you also checked that it actually contains mapped entities ?
Ok, I found the error, I was missing --force parameter, first step command should be:
php app/console doctrine:mapping:convert yml ./src/MyShop/ProductBundle/Resources/config/doctrine/metadata/orm --from-database --force --filter="Product" --em=mysecondaryem

Resources