Welcome Guest, Not a member yet? Register   Sign In
Types in data models
#1

(This post was last modified: 12-25-2022, 07:43 AM by ruslan.)

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
Reply
#2

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...tity-usage
Reply
#3

(This post was last modified: 12-25-2022, 07:55 AM by ruslan.)

(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...tity-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
Reply
#4

(This post was last modified: 12-25-2022, 10:12 AM by luckmoshy.)

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
Codeigniter First, Codeigniter Then You!!
yekrinaDigitals

Reply
#5

(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.
Reply
#6

(This post was last modified: 11-22-2023, 07:40 PM by kenjis.)

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

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

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

If you have interest, try and/or review.
Reply
#9

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




Theme © iAndrew 2016 - Forum software by © MyBB