Copy Paste entities from a project to another symfony - symfony

I have a projet that includes many entities and their controller and crud, etc
I want to reuse them in another projet. So I want to copy paste the files.
But I already have a problem at entity creation:
The cmd shows that make: entity creates the entity class and the repository class (and nothing else) .So I copy paste these two to the new projet but they are ignored:
When I try make:migration, I got No database changes were detected. When I try make:entity and type the name of the entity class (Taxe), I got _Cannot find the entity manager for class "App\Entity\Taxe" . and make:controller with Taxe as entity return _ Entity "Taxe" doesn't exist; .
So globally, how to create entity without the maker ? How can I reuse my entities/controllers/etc from another project pls?

Globally, to reuse your controllers/entities, just copy/paste them in the right folder.
Entities : src/Entity
Controllers : src/Controller
Repositories: src/Repository
After that (if your database is created, else run php bin/console doctrine:database:create) , you can make php bin/console make:migration and php bin/console doctrine:migrations:migrate to apply changements.
The cmd shows that make: entity creates the entity class and the repository class (and nothing else)
If you want to generate templates, you have to run php bin/console make:controller, name your controller and controller file + template will be generated (this implies that your project is a web app and not an api).

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.

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

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 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