CodeIgniter Forums

Full Version: problem with response and entity
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How do you do guys ? nice ? ok 'cause I have a problem Big Grin

if my model return and object Entity, If i ask in my controller the typeof $entity->createdAt, it answer CodeIgniter\I18n\Time Object ok nice (magical mutations and casts works) BUT....

if i do $this->response->SetJson($entity); i got an empty object. no cast, no magical conversions, all fields are protected so the json_encode can't access to this fields. The same with a var_dump or print_r. fields are not converted.

How can i send over a REST application the correct representation of my entity ?

Note :

If i put some fields"public" in my entity, i can see it in my response but without magical conversion...

is that a bug or a workaround ? maybe someone did this trick ?

the worst is that if i do $this->response->SetJson($entity->created_at); I've got an error 500 about a problem with format.

Argument 1 passed to CodeIgniter\Format\JSONFormatter::format() must be of the type array, object given, called in /www/api/system/HTTP/Response.php on line 551
Have you tried "return $this->respond($myentity);"? if $myentity is an associative array, it should be returned as JSON.
If it isn't an array, you could cast it as such, or use the Model's asArray() method as part of the chain when you retrieve the entity.
Call to undefined method App\Controllers\Users::respond() :S
Is this meant to be a resource controller, eg an API endpoint?
If so, I recommend using the API response trait, which is where that method comes from.

This is a work-in-progress.
my bad, bad use of ResponseTrait.

return $this->respond($users_data, 200);


ok I will take a look tomorow. thx for the link
well... that the same


PHP Code:
namespace App\Controllers;

// use CodeIgniter\Controller;
use CodeIgniter\API\ResponseTrait;



Users extends ResourceController

    public function 
$userModel  = new UserModel();
$users_data $userModel->findAll();

        if (
$this->fail('No Users'404);
    // End of file Users.php

i will try to make a custom entity and a sort of toString() or toJson() method who will read the protected vars and reconstruct a correct object.

a json_encode() si not enough for that kind of object.
yeepee !!!!

        "id": "1",
        "firstname": "Darth",
        "lastname": "Vader",
        "username": null,
        "email": "[email protected]",
        "password": "5e884898da28047151d0e56f8dc6292",
        "created_at": {
            "date": "2019-02-08 12:27:42.000000",
            "timezone_type": 3,
            "timezone": "Europe/Paris"
        "updated_at": null,
        "deleted": false,
        "deleted_at": null

so basically, i wrote a toArray() method

PHP Code:
public function toArray(){
$vars get_object_vars($this);
$ar = [];
$vars as $k => $v){
            if (
$k[0] != '_'){
$ar[$k] = $this->__get($k);

and VOILA !

so i will wrote MY_Entity with many converters json, xml, yaml maybe ....

and my own MY_Model who will call my converters after find.

and maybe something like that.

And I saw that call inside a model $this->created_at is not the same as $this->__get('created_at').
Is that the normal behavior ?