CodeIgniter Forums
problem with response and entity - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: problem with response and entity (/thread-72909.html)



problem with response and entity - keulu - 02-27-2019

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.

Code:
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



RE: problem with response and entity - ciadmin - 02-27-2019

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.


RE: problem with response and entity - keulu - 02-27-2019

Call to undefined method App\Controllers\Users::respond() :S


RE: problem with response and entity - ciadmin - 02-27-2019

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.

See https://github.com/codeigniter4/CodeIgniter4/blob/feature/resource/system/Resource/ResourceController.php
This is a work-in-progress.


RE: problem with response and entity - keulu - 02-27-2019

my bad, bad use of ResponseTrait.

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

->

<response/>


RE: problem with response and entity - keulu - 02-27-2019

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


RE: problem with response and entity - keulu - 02-28-2019

well... that the same

Code:
[
    {}
]

PHP Code:
<?php
namespace App\Controllers;

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

use 
Restserver\Libraries\Format;

use 
App\Models\UserModel;
use 
App\Entities\User;

class 
Users extends ResourceController
{
    use 
ResponseTrait;

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

        if (
$users_data)
        {
            return 
$this->respond($users_data);
        }
        else
        {
            return 
$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.


RE: problem with response and entity - keulu - 02-28-2019

yeepee !!!!

Code:
[
    {
        "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 = [];
        foreach(
$vars as $k => $v){
            if (
$k[0] != '_'){
                
$ar[$k] = $this->__get($k);
            }
        }
        return 
$ar;
    } 

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 ?