doctrine:generate:entities vs generate:doctrine:entities in Symfony2 console - symfony

What are the differences between
doctrine:generate:entities vs generate:doctrine:entities
the description for both is the same
Generates entity classes and method stubs from your mapping information

They are the same.
generate:doctrine:entities is an alias to doctrine:generate:entities
In the DoctrineBundle source code it is coded as:
...
$this
->setName('doctrine:generate:entities')
->setAliases(array('generate:doctrine:entities'))
...
Source code link.

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

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

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

Resources