CodeIgniter Forums
Types in data models - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Feature Requests (https://forum.codeigniter.com/forumdisplay.php?fid=29)
+--- Thread: Types in data models (/showthread.php?tid=85956)



Types in data models - ruslan - 12-25-2022

Hello

It would be nice to have automatic parsing of base types for data model operations
For example i have this table:
Code:
CREATE TABLE `setting` (
  `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(50),
  `extraData` JSON,
  PRIMARY KEY (`id`) USING BTREE,
);
I would like SettingModel`s find() and findAll() methods to return already typed objects, like:
Code:
{ "id: 1, "name": "Alex", "extraData": {"someData": "someValue"}}
Right now it's all strings and i need to use $afterFind and $beforeInsert hooks to normalize data
It might be convinient to have optional protected
Code:
$types 
property in the
Code:
CodeIgniter\Model 
class with syntax like something that:
PHP Code:
$types = [
   "number" => ["id"],
   "json" => ["extraData"]
];

I can implement this feature 
a

afterFind

afte


RE: Types in data models - ozornick - 12-25-2022

Use Entity class with cast functions. Apply $returnType in models SomeEntity::class
For return all properties typed use method toArray()
https://codeigniter4.github.io/userguide/models/entities.html#entity-usage


RE: Types in data models - ruslan - 12-25-2022

(12-25-2022, 05:49 AM)ozornick Wrote: Use Entity class with cast functions. Apply $returnType in models SomeEntity::class
For return all properties typed use method toArray()
https://codeigniter4.github.io/userguide/models/entities.html#entity-usage

I don't need and don't want to use `Entity` class
That's much of extra code and overcomplication for a simple natural feature

cl
Code:
class User extends Entity
{
    protected $casts = [
        'options'        => 'array',
        'options_object' => 'json',
        'options_array'  => 'json-array',
    ];
}
This casting from Entity can be used in Model


RE: Types in data models - luckmoshy - 12-25-2022

it is a good idea and of cause @ruslan even i certain days stuck on the same issue but unfortunately, CI has no this way unless you PR to it


RE: Types in data models - ruslan - 12-25-2022

(12-25-2022, 10:11 AM)luckmoshy Wrote: it is a good idea  and of cause @ruslan  even i certain days stuck on the same issue but unfortunately, CI has no this way unless you PR  to it

I do want to PR to it
Just asking for the opinion of the main CI developers on this and checking if there is something already created for this that i might be missing.


RE: Types in data models - kenjis - 11-22-2023

I'm trying to implement like this feature.
https://github.com/codeigniter4/CodeIgniter4/pull/8230
and the upcoming next PR.


RE: Types in data models - kenjis - 12-02-2023

Model field casting
https://github.com/codeigniter4/CodeIgniter4/pull/8243


RE: Types in data models - kenjis - 02-03-2024

This PR is ready to review: https://github.com/codeigniter4/CodeIgniter4/pull/8243

If you have interest, try and/or review.


RE: Types in data models - kenjis - 02-13-2024

I think this PR is ready to merge.
https://github.com/codeigniter4/CodeIgniter4/pull/8243
If you have interest, try it.