CodeIgniter Forums
changing model data before returning it? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: changing model data before returning it? (/showthread.php?tid=84912)



changing model data before returning it? - rramsey - 11-22-2022

I have a basic People table with name, created_at, updated_at, and deleted at columns.  I have People Entity and PeopleModel classes set up correctly(I think).  In my controller I do:

PHP Code:
public function getPeople() {
    try {
        $peopleModel = new PeopleModel($this->db);
        $this->response->setJSON$peopleModel->
            where('name''John;)->
            orderBy('
name', 'ASC')->
            findAll()
        );
        $this->response->send();
        exit;
    } catch (\Exception | \Throwable $e) {
        return handleError($e, "PeopleController::getPeople" );
    }

This works fine.  I get a list back of everyone named John, no problem.  But, I also get this for the created_at value:
Code:
"created_at": {
    "date": "2022-11-22 08:55:22.000000",
    "timezone_type": 3,
    "timezone": "America/Chicago"
},


What I want is just the simple datetime value:  "created_at": "2022-11-22 08:55:22" not an object.  The db doesn't store milliseconds either and I'm not using them.
I've tried a getter in the Entity because it wasn't 100% clear the getter only fired if I tried to get the value outside of the model.  I tried setting up an afterFind callback modeled after the beforeFind in the docs, but the data didn't really fit the example.  I tried 
PHP Code:
if (isset($data['name']) ) {
    $data['name']      "Johnny";
    $data['data']['name'] = "Asdf";
    $data['data'][0]['name'] = "foobar';
    
$data['returnData'] = true;
    return 
$data;


And a few more variations, but no luck.

Is there a built-in way to get back:
Code:
{
    "name": "John", 
    "created_at": "2022-11-22 08:55:22.000000"
},

Thanks!