Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New entities created with only a controller in PHP; no service, repository, entity, etc. #2873

Closed
bandtank opened this issue Oct 8, 2023 · 9 comments

Comments

@bandtank
Copy link
Contributor

bandtank commented Oct 8, 2023

Describe the bug
It looks like 8.0 added entityTemplates, which are supposed to assist with the creation of entities based on type (base, base plus, etc.).

To Reproduce (MANDATORY, DO NOT REMOVE)
Explicit steps to reproduce the behavior:

  1. Use V8.0.0 or later
  2. Create a new entity called Topic in the Entity Manager through the GUI
  3. Inspect the files in custom/Espo/Custom to see that only a controller has been created

Expected behavior
I was expecting to see:

  • Controllers/Topic.php
  • Entities/Topic.php
  • Repositories/Topic.php
  • Services/Topic.php

However, only Controllers/Topic.php was created.

As an aside, the following json files were also created, which is consistent with my past experience:

  • Resources/i18n/en_US/Topic.json
  • Resources/metadata/clientDefs/Topic.json
  • Resources/metadata/clientDefs/Topic.json
  • Resources/metadata/entityDefs/Topic.json
  • Resources/metadata/recordsDefs/Topic.json
  • Resources/metadata/scopes/Topic.json

EspoCRM version
V8.0.0 or later

Additional context
After reviewing the code in application/Espo/Tools/EntityManager/EntityManager.php, I do not see a call to the template to create new files. Is this intentional? Is the controller the only file that should exist for a new entity of type base?

@yurikuzn
Copy link
Contributor

yurikuzn commented Oct 8, 2023

It's intentional.

@yurikuzn yurikuzn closed this as not planned Won't fix, can't repro, duplicate, stale Oct 8, 2023
@bandtank
Copy link
Contributor Author

bandtank commented Oct 8, 2023

Ok, sounds good. Thanks. Is it fine for the developer to create the files if they are needed? For example, Entities/Topic.php may be needed so I can use Topic::ENTITY_TYPE in code. Will Espo still use the file if it is created manually?

@rabiibrahimi
Copy link
Contributor

yeah you can create the files manually, i always do that so i could defined setters and getters on the Entities/MyEntity.php

You can even create you own template if you want to and it will be merged if you added everything correctly. i have used in many project a hybride entity template BasePlusEvent which is composed of both base plus and event.

@bandtank
Copy link
Contributor Author

bandtank commented Oct 8, 2023

That did not work for me. I added this file:

{
  "Base": {
      "entityClassName": "Espo\\Core\\Templates\\Entities\\Base"
  }
}

and then created a new Base entity called Test. I cleared the cache after adding the entityTemplates.json file to custom/Espo/Custom/Resources/metadata/app/. I expected to see two new classes (controller and entity), but only the controller was created.

I added a debug statement to be sure the file is being seen in site/application/Espo/Tools/EntityManager/EntityManager.php:

        $templateDefs = $this->metadata->get(['app', 'entityTemplates', $type], []);
        $GLOBALS["log"]->debug("templateDefs",[$templateDefs]);

which resulted in the following statement in the log (why did it show a repositoryClassName?:

[2023-10-08 16:55:04] DEBUG: templateDefs [{"entityClassName":"Espo\\Core\\Templates\\Entities\\Base","repositoryClassName":"Espo\\Core\\Templates\\Repositories\\Base"}] 

Requests to create new entities go here: site/application/Espo/Controllers/EntityManager.php. The create method has no calls other than this:

$this->entityManagerTool->create($name, $type, $params);

The create function in entityManagerTool does not create the template-based files. I don't see how this process would create the template-based classes unless other steps need to be followed.

Edit

Something must be wrong. As seen above, there shouldn't have been a repositoryClassName in the log because I didn't define one in my entityTemplates.json file. A default entityTemplates.json file already exists with the templates listed for the default entity types in site/application/Espo/Resources/metadata/app/:

{
    "Base": {
        "entityClassName": "Espo\\Core\\Templates\\Entities\\Base",
        "repositoryClassName": "Espo\\Core\\Templates\\Repositories\\Base"
    },
    ...
}

Espo ignored my entityTemplates.json file and it did not create the new entity or repository classes.

@yurikuzn
Copy link
Contributor

yurikuzn commented Oct 9, 2023

Everything in Espo works fine as it was designed. You shouldn't create entityTemplates.json, I wouldn't recommend doing it. Here's the entity class provider.

Just create an entity class in your module directory (or custom, but I recommend moving all to the module) and clear cache.

@yurikuzn
Copy link
Contributor

yurikuzn commented Oct 9, 2023

With JSON schema it's also easy to create entities manually. I do it manually. All classes, metadata, layouts, language.

@bandtank
Copy link
Contributor Author

bandtank commented Oct 9, 2023

I don't mind making everything manually. The default json file says this, though:

{
    "Base": {
        "entityClassName": "Espo\\Core\\Templates\\Entities\\Base",
        "repositoryClassName": "Espo\\Core\\Templates\\Repositories\\Base"
    },
    ...
}

and only a controller is created. The file is not being used.

@yurikuzn
Copy link
Contributor

yurikuzn commented Oct 10, 2023

This metadata is used by the class provider I referenced above. It has nothing to do with Entity Manager Tool.

@yurikuzn
Copy link
Contributor

yurikuzn commented Oct 10, 2023

Whenever I want to quickly check where in codebase something is used I press Ctrl + Shift + F (in PhpStorm), paste the name (e.g. repositoryClassName) and it finds all occurrences instantly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants