[SOLVED] CI4 Entity using datamap to Model-Save |
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 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; Encounter problem while building new data using the method below: PHP Code: $newActivity = new ActivityEntity(); 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.
Did you create an Entity folder in the app folder?
If you did then you access them like this. PHP Code: // change this What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(07-15-2021, 12:30 AM)InsiteFX Wrote: Did you create an Entity folder in the app folder?...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'];
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 )
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. |
Welcome Guest, Not a member yet? Register Sign In |