Skip to content

Bug: Model class not found for entities with suffix #3

Description

@rakoitde

Describe the bug
When testing the example "Updating an Existing Record" for "Has One" relations with an entity with a suffix (UserEntity), the model name cannot be resolved.

Michalsn\CodeIgniterRelations\Exceptions\RelationException: Model class not found for entity App\Entities\UserEntity
VENDORPATH/michalsn/codeigniter-relations/src/Traits/HasEntityWrites.php(49): Michalsn\CodeIgniterRelations\Exceptions\RelationException::forModelClassNotFound('App\\Entities\\UserEntity')
APPPATH/Models/BaseModel.php(96): App\Entities\UserEntity->save()

To Reproduce

Create model and entity with suffix and try to update existing data.

php spark make:model User --suffix --return entity

https://michalsn.github.io/codeigniter-relations/relations/one-to-one/#updating-an-existing-record

I was able to solve that by removing the suffix from the $baseClassName in
/vendor/michalsn/codeigniter-relations/src/Traits/HasModelDiscovery.php

$baseClassName = substr((string) strrchr($className, '\\'), 1);
$baseClassName = str_replace('Entity', '', $baseClassName);  // <---- remove "Entity"
$modelPath     = str_replace('Entities', 'Models', $baseNamespace) . '\\' . $baseClassName . 'Model';

Expected behavior
Model names can be resolved to entities with and without suffixes.

Environment (please complete the following information):

  • PHP 8.5.2
  • Apache
  • CodeIgniter 4.7.2

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions