Welcome Guest, Not a member yet? Register   Sign In
Compatibility DB field JSON with Codeigniter Entity
#1

Hi,
I have a Mysql DB with JSON FIELDS for example:
Code:
CREATE TABLE `contents` (
`content_id` int(11) NOT NULL AUTO_INCREMENT,
`gallery` json DEFAULT NULL
)

My content Entity is:
<?php

namespace App\Entities;

use CodeIgniter\Entity\Entity;

class Content extends Entity
{

    protected $casts = [
        'content_id' => 'integer',
        'gallery' => 'json'
    ];
}

My model
<?php

namespace App\Models;

use CodeIgniter\Model;

class ContentModel extends Model
{
    ...
    protected $returnType = \App\Entities\Content::class;

    public function customFind($where = [], $select = [])  {
        $bulder = $this->db->table("contents");
        $bulder->select("*");

        return $bulder->get()->getResultObject(\App\Entities\Content::class);
   }
   
}

When in Codegniter call method "getResultObject" and set the cast of attibute "gallery" call function:
json_encode("gallery", JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR)

But the field "gallery" is alredy encoded in the the $builder result.
Is it possible to introduce a check in the class "JsonCast" to see if the string isn't already encoded?
Or introduce in the getResultObject method an options for "skip" _set trasformation of specific field ex: getResultObject(\App\Entities\Content::class, ["skipSet"=>["gallery"]])
thank you!
Reply
#2

The BaseResult::getResultObject() method does not expect any parameters.

All classes inherited from CodeIgniter\Entity\Entity keep the data "as is" without any type casting.
Reply
#3

(03-24-2023, 12:21 AM)iRedds Wrote: The BaseResult::getResultObject() method does not expect any parameters.

All classes inherited from CodeIgniter\Entity\Entity keep the data "as is" without any type casting.

I would add that if necessary just add a custom callback to "find" in order to get the data already formatted.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB