magento2-code-generator icon indicating copy to clipboard operation
magento2-code-generator copied to clipboard

question: how to generate multiple entities with crudEAV in the same module

Open ioweb-gr opened this issue 4 years ago • 3 comments

While trying to execute

  • t:g generate with EntityName1
  • t:g generate with EntityName2

Multiple files are going to the same paths, thus causing to overwrite the previously created data and breaking the previous entity.

For example

1. the collection and eav attribute

  • app/code/Vendor/Module/Model/ResourceModel/Attribute/Collection.php (this relies on the entity name)
  • app/code/Vendor/Module/Model/ResourceModel/Eav/Attribute.php (this relies on the entity name)

Could these two be named based on the EntityName like EntityNameCollection and EntityNameAttribute ? They only seem to be used in the EntityNameSetup file.

2. the resource model attribute

  • app/code/Vendor/Module/Model/ResourceModel/Attribute.php (this also uses internally the entity name )

3. the configuration files which create a lot of data

├── etc │   ├── acl.xml │   ├── adminhtml │   │   ├── menu.xml │   │   └── routes.xml │   ├── di.xml

4. the setup code.

Setup/ ├── EavTablesSetup.php ├── EntityName1Setup.php ├── InstallData.php ├── InstallSchema.php ├── EntityName2Setup.php └── Uninstall.php

I'm wondering if there's a way to make it merge the files or if not would it be possible to get a second option to append (maybe commented out) the code to the existing files instead of overwriting?

That way it is going to be easier to repair the files that need to be adjusted. Pretty much copy paste the code in the relevant parts from the commented out areas

ioweb-gr avatar Apr 09 '20 16:04 ioweb-gr

Hello @ioweb-gr

Unfortunately there is no functionality to merge both files, that would be too difficult to implement.

The tool handles that in a different way. If a file already exists, you are asked for confirmation to overwrite the file. If you reply "no", the file is not overwritten and the code is displayed on the output. You can copy that code and paste it manually in the corresponding file.

I worked a lot with this tool in the past and never needed that, though. I would suggest that if you need 2 entities, you could consider to have 2 modules. One for each entity.

Hope that helps!

jalogut avatar Apr 09 '20 20:04 jalogut

Hi @jalogut, thanks for the reply. I can see it's difficult in Magento 2 yeah :(

I guess I got a bit used to symfony's generator bundle which can append stuff to entities if needed. It's a bit more robust in this regard. Doctrine annotations help it greatly with the db schema updates but in magento everything has to be declared explicitly.

I didn't know it outputs the classes if you set override to no. That's really helpful. I guess it's easy to put the config parts manually, it's no biggie.

The EAV files are a bit complex to merge. I think part 1 and 2 could be fixed relatively easily with minor changes if we prepended the EntityName in the classes or by creating a subfolder for them. Then alter the paths wherever they are referenced. That would make sure there are no duplicates there and no overwrite would be needed. It would also be more explanatory which file belongs to which entity.

e.g.

  • app/code/Vendor/Module/Model/ResourceModel/Attribute/EntityName1Collection.php or app/code/Vendor/Module/Model/ResourceModel/Attribute/EntityName1/Collection.php
  • app/code/Vendor/Module/Model/ResourceModel/Eav/EntityName1Attribute.php or app/code/Vendor/Module/Model/ResourceModel/Eav/EntityName1/Attribute.php

I'm not sure if anything could be done for the setup classes though. Maybe the table and column generation could be done via a db_schema.xml file instead to provide easier merging.

Unfortunately for my case I will require more than 4 entities for this module I'm building so it doesn't make sense to split it in 4 modules just to put the entities there, that's why I was hoping I could work around this issue.

I was thinking of putting some effort in helping out with the templates here to make future life easier too instead of merging all those entities manually, if I could get some guidance to make them work better with multiple entities.

ioweb-gr avatar Apr 09 '20 20:04 ioweb-gr

I was able to create a new private template that could handle the situation for multi EAV CRUD entities with minimal merging. I'll test it a bit longer and submit a pull request to discuss it and see if people want to merge it.

ioweb-gr avatar Apr 23 '20 07:04 ioweb-gr