Specify entity name while generating from existing database table symfony2 - symfony

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

Related

Symfony 4 and Doctrine, how to generate repository automatically after mapping?

All the tutorials I am finding have the repository created automatically using make:entity when creating new tables
but I have been importing from an existing database following the official documentation with the following command: php bin/console doctrine:mapping:import App\\Entity annotation --path=src/Entity
This command does not seem to create any repository... and the documentation does not talk about generating a repository
I know I can create one manually but is there a command to generate them automatically ? I have 25 tables.... Would be very useful
lazy me oO
edit: I also tried php bin\console make:entity --regenerate but I get no change on all tables and no repository created
SOLUTION 1
You can simply run
php bin/console make:entity --regenerate
This will prompt and ask for:
Enter a class or namespace to regenerate [App\Entity]:
Just press Enter or specify the location of your entity folder, and it will create missing getters/setters & Repositories.
---> WARNING:
If it does not create the repositories make sure you have the following annotation in your entities :
/**
* #ORM\Entity(repositoryClass="App\Repository\MyClassRepository")
*/
class MyClass
{
}
You also might want to clear your cache if it's not working (as noted by #Pavel Petrov in the comments)
SOLUTION 2
The SymfonyMakerBundle allows you to create your own makers. So you could make a new one called make:repositories that will generate a repository for each entity found in the /Entity folder.
To do that, create a class (MakeRepositories) that extends AbstractMaker in your src/Maker/ directory. (documentation: https://symfony.com/doc/current/bundles/SymfonyMakerBundle/index.html#creating-your-own-makers)
Use the core maker make:entity to help you create your new command (since it contains the code to generate a repository) : https://github.com/symfony/maker-bundle/blob/master/src/Maker/MakeEntity.php
After generating your entity classes from database, add the following annotation to each of your entities:
#ORM\Entity(repositoryClass="App\Repository\ClassNameRepository")
To genenerate the repository classes, run the following command:
php bin/console make:entity --regenerate App
How to Generate Entities from an Existing Database
Table name: CamelCase (eg: table_name will be TableName)
php bin/console doctrine:mapping:import App\\Entity annotation --path=src/Entity --filter="TableName"
How to Generate Entities
Run below command, it will create entity file.
php bin/console make:entity --regenerate
Next, go to your entity file and add #ORM\Entity repositoryClass
Example Entity file
/**
* XXXXXX
*
* #ORM\Table(name="XXXX")
* #ORM\Entity(repositoryClass="App\Repository\XXXXRepository")
*/
class XXXXX {
Run again this command again, and it will create repository for you.
php bin/console make:entity --regenerate
Do not copy the annotation too fast, I copied the annotation, but in my case the entities were generated automatically, so there was already a line ORM\Repository, which appeared after, delete it or replace it with the solution

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

Steps in generating entity

I need help to generate entity, because when I tried the below one I get errors.
Create databse:
php app/console doctrine:database:create
Generate entity:
php app/console doctrine:generate:entity
Generate the Getters and Setters:
php app/console doctrine:generate:entities AiQABlogBundle/Entity/Profile
The above step gives this error:
[RuntimeException]
Namespace "AiQABlogBundle\Entity\Profile" does not contain any mapped entities.
In this post you find answer
Or You should enter this command
php app/console doctrine:generate:entities AiQABlogBundle:Profile

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