Symfony2 generate entities error - symfony

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

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.

How to specify a destination directory in Generate Entities with Doctrine2

Im using symfony framework with Doctrine as ORM.
When i run this command:
php bin/console doctrine:mapping:convert annotation ./src --env=local --em=myentitymanager
I get this output:
Processing entity "AppBundle\Entity\Offer"
Processing entity "AppBundle\Entity\Product"
I need to know if exist some argument or some way to create the entities in a subdirectory inside AppBundle/Entity.
So is any way to get this output?
Processing entity "AppBundle\Entity\Exampledir\Peticion"
Processing entity "AppBundle\Entity\Exampledir\Tecnologia"
Unfortunately the php bin/console doctrine:mapping:convert command does not accept a parameter to define the entity directory.
The target folder of the bundle has been hardcoded in the source code as:
if you choose annotation it's /Entity
if you choose xml, it's /Resources/config/doctrine

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

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.

Specify entity name while generating from existing database table symfony2

I've a database table users and I want to generate its entity in my symfony2 bundle KapCrudBundle.
I'm using the following commands:
php app/console doctrine:mapping:convert yml ./src/Kap/CrudBundle/Resources/config/doctrine/metadata/orm --from-database --force --filter=Users
php app/console doctrine:mapping:import KapCrudBundle annotation --filter=Users
php app/console doctrine:generate:entities KapCrudBundle
These all above commands working absolutely fine but I want to do the following things:
It's generating the entity Users.php and annotation file Users.orm.yml because my table name is users but I want the entity name and annotation files like User.php and User.orm.yml
Second thing is that it generates all entities again in the bundle but I want only the User entity to generate.
Regarding the 2nd question: you can simply specify the entity.
php app/console doctrine:generate:entities KapCrudBundle/Entity/User

Resources