CodeIgniter Forums
Entity class not working on 2 module - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Entity class not working on 2 module (/showthread.php?tid=77766)



Entity class not working on 2 module - tmtuan - 10-15-2020

hi all,

i recently implement entity to my project, i successfull setup it to work with the backend module, but when i call that model in the frontend module, it does not return an entity object, i return the normal object. Here is my project structure and code

my folder structure 
[Image: eb-err-entity.png]

when i run and debug the code in acp module
[Image: eb-err-entity-2.png]
as you can see, i got my post item return successfully in entity object

but when i call it in my site module
[Image: eb-err-entity-1.png]
it does not return entity object but a normal object, does any one have this problem? or i missing something?


RE: Entity class not working on 2 module - InsiteFX - 10-15-2020

Your not giving enough information like what is site a module?

If your calling a Site Module then you need to tell it where the Entity is.

use app/Modules/Acp/Entities/entityName;


RE: Entity class not working on 2 module - tmtuan - 10-16-2020

(10-15-2020, 11:27 PM)InsiteFX Wrote: Your not giving enough information like what is site a module?

If your calling a Site Module then you need to tell it where the Entity is.

use app/Modules/Acp/Entities/entityName;

as i mention in my structure, acp is a module stand for admin panel and site is a module stand for frontend

in both controller acp/post and site/post i use just 1 model: Modules\Acp\Models\PostModel; ( as you can see in the 2,3 picture )

and there are 2 different result, the result in Acp/post controller give me the entity object but the result in Site/post controller return the normal object


RE: Entity class not working on 2 module - InsiteFX - 10-16-2020

Our you defining the use clause like I showed?

If the Entity is in the acp then the use clause should be like below.

Site/Post controller
use app/Modules/Acp/Entities/entityName;
use app/Modules/Acp/Models/PostModel;

Is that how you have your Site controller?


RE: Entity class not working on 2 module - tmtuan - 10-16-2020

(10-16-2020, 11:19 AM)InsiteFX Wrote: Our you defining the use clause like I showed?

If the Entity is in the acp then the use clause should be like below.

Site/Post controller
use app/Modules/Acp/Entities/entityName;
use app/Modules/Acp/Models/PostModel;

Is that how you have your Site controller?

yes, you're right, in my Site/Controller i used: use app/Modules/Acp/Models/PostModel; and call that model to load data

and in my postModel code i return Entity class, my code is like this:


PHP Code:
namespace Modules\Acp\Models;

use 
CodeIgniter\Model;
use 
Modules\Acp\Entities\Post;

class 
PostModel extends Model
{
    protected $table 'post';
    protected $primaryKey 'id';

    protected $returnType Post::class;
    protected $useSoftDeletes true


With this code when i call the model in Acp/Controller it successfully return the Entity class

I also try to create new PostModel in Site/Model and new Entity in Site/Entities, my new model code is like:


PHP Code:
namespace Modules\Site\Models;

use 
CodeIgniter\Model;
use 
Modules\Site\Entities\Post;

class 
PostModel extends Model
{
    protected $table 'post';
    protected $primaryKey 'id';

    protected $returnType  'Modules\Site\Entities\Post';
    protected $useSoftDeletes true

but when i try to call the new model it does not return the entity object, i got the same problem as above. I dont know what wrong...


RE: Entity class not working on 2 module - InsiteFX - 10-16-2020

To me it looks like your name space is off because your Modules folder is under the app folder.

PHP Code:
namespace Modules\Site\Models;

// should be
namespace app\Modules\Site\Models

So all your namespaces and use clauses should start with app\


RE: Entity class not working on 2 module - tmtuan - 10-16-2020

(10-16-2020, 07:46 PM)InsiteFX Wrote: To me it looks like your name space is off because your Modules folder is under the app folder.

PHP Code:
namespace Modules\Site\Models;

// should be
namespace app\Modules\Site\Models

So all your namespaces and use clauses should start with app\
that namespace work perfect with my acp and other stuff, because i declare my module in my autoload like this:
PHP Code:
$psr4 = [
            
'App'         => APPPATH,                // To ensure filters, etc still found,
            
APP_NAMESPACE => APPPATH,                // For custom namespace
            
'Config'      => APPPATH 'Config',
            
'Modules\Site' => APPPATH 'Modules/Site',
            
'Modules\Acp' => APPPATH 'Modules/Acp',
            'Modules\Auth' => APPPATH 'Modules/Auth',
        ]; 



RE: Entity class not working on 2 module - InsiteFX - 10-17-2020

Then I it looks like you may have found a bug in the Entities, I would file an issue
on GitHub for it.


RE: Entity class not working on 2 module - tmtuan - 10-17-2020

(10-17-2020, 04:18 AM)InsiteFX Wrote: Then I it looks like you may have found a bug in the Entities, I would file an issue
on GitHub for it.
i also think my Entities have problem, so i double check it and run debug, but there's no problem on it, because the entities work good when i call it in Modules\Acp, all function work well in it

It just have the problem when i use in Modules\Site