![]() |
Compatibility DB field JSON with Codeigniter Entity - 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: Compatibility DB field JSON with Codeigniter Entity (/showthread.php?tid=87174) |
Compatibility DB field JSON with Codeigniter Entity - condorman - 03-22-2023 Hi, I have a Mysql DB with JSON FIELDS for example: Code: CREATE TABLE `contents` ( 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! RE: Compatibility DB field JSON with Codeigniter Entity - iRedds - 03-24-2023 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. RE: Compatibility DB field JSON with Codeigniter Entity - Valkhan - 09-04-2023 (03-24-2023, 12:21 AM)iRedds Wrote: The BaseResult::getResultObject() method does not expect any parameters. I would add that if necessary just add a custom callback to "find" in order to get the data already formatted. |