Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] CI4 Entity using datamap to Model-Save
#1

(This post was last modified: 07-16-2021, 03:18 AM by monkenWu.)

Hi, I have some question to the usage of Entity.
For example, I have a entity class `ActivityEntity` as below:

PHP Code:
class ActivityEntity extends Entity
{
    protected $attributes = [
        'key' => null,
        'userKey' => null,
        'name' => null,
        'archivedAt' => null,
        'createdAt' => null,
        'updatedAt' => null,
        'deletedAt' => null,
    ];

    protected $datamap = [
        'user_key' => 'userKey',
        'archived_at' => 'archivedAt',
        'created_at' => 'createdAt',
        'updated_at' => 'updatedAt',
        'deleted_at' => 'deletedAt'
    ];



Since the project obeys the Camel-case, I re-map the column name of the database into Camel-case using `datamap` just like how the documentation describes.
And my `ActivityModel` looks like:

PHP Code:
use App\Entities\ActivityEntity;
class 
ActivityModel extends Model
{
    protected $DBGroup 'default';
    protected $table 'activity';
    protected $primaryKey 'key';
    protected $returnType ActivityEntity::class;
    protected $protectFields true;
    protected $allowedFields = ["user_key""name""archived_at"];

    protected $useSoftDeletes true;
    protected $useTimestamps true;
    protected $dateFormat 'datetime';
    protected $createdField 'created_at';
    protected $updatedField 'updated_at';
    protected $deletedField 'deleted_at';



Encounter problem while building new data using the method below:

PHP Code:
$newActivity = new ActivityEntity();
$newActivity->userKey 1;
$newActivity->name 'activity name';
$activityModel = new ActivityModel();
$activityModel->save($newActivity); 

I was expecting the upper codes will insert a new activity data into the database, however it went wrong. The reason is that when the `save` method processing `ActivityEntity` I passed into, it didn't follow the `datamap` setting to turn `userKey` into `user_key`, hence the database is unable to write normally.

I tried to call the `toArray` method within the Entity and found out the setting of `datamap` really does work, my `userKey` turns into `user_key`, but the auto-transformation I expected did not happen in the Model's `insert` and `save`, which stopped me to deal with `Entity` and create `Activity` through Camel-case.

I'm not sure whether its an error or not, or I misunderstood how to use Entity.
Reply
#2

Did you create an Entity folder in the app folder?
If you did then you access them like this.
PHP Code:
// change this
protected $returnType ActivityEntity::class;

// to this
protected $returnType 'App\Entities\ActivityEntity::class';

// change this
$newActivity = new ActivityEntity();

// to this
$newActivity = new \App\Entities\ActivityEntity(); 
Also did you setup the Entity to use the correct get and set methods?
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(07-15-2021, 12:30 AM)InsiteFX Wrote: Did you create an Entity folder in the app folder?
If you did then you access them like this.
PHP Code:
// change this
protected $returnType ActivityEntity::class;

// to this
protected $returnType 'App\Entities\ActivityEntity::class';

// change this
$newActivity = new ActivityEntity();

// to this
$newActivity = new \App\Entities\ActivityEntity(); 
Also did you setup the Entity to use the correct get and set methods?
...Oh, I think I found the problem.
User guide and $datamap comment tell me:
PHP Code:
$datamap = ['db_name' =>'class_name']; 

But it should actually be:
PHP Code:
$datamap = ['class_name' =>'db_name']; 
Reply
#4

Thanks for reporting the problem @monkenWu. Would you open a Pull Request for us at GitHub to fix the User Guide?
Reply
#5

(07-15-2021, 04:35 AM)MGatner Wrote: Thanks for reporting the problem @monkenWu. Would you open a Pull Request for us at GitHub to fix the User Guide?

I will create a Pull Request later.
Reply
#6

Also please add [SOLVED] to your topic title for other users to see.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

I have created a PR: https://github.com/codeigniter4/CodeIgniter4/pull/5497

It might be fixed after the original post, the User Guide says:

> The key of the array is class property to map it to, where the value in the array is the name of the column in the database.

It is correct. But $attributes should be the name of the column in the database.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB