Sooooo... I had a difference beetween my $returnType ( returnType = "\App\Entities\User"; ) and lonnieezell User Entity ( returnType = User::class; ) but it makes no difference.
In my controller :
var_dump( var_dump($data['data'][0]->created_at); )
Code:
object(CodeIgniter\I18n\Time)#62 (6) {
["timezone":protected]=>
object(DateTimeZone)#64 (2) {
["timezone_type"]=>
int(3)
["timezone"]=>
string(12) "Europe/Paris"
}
["locale":protected]=>
string(2) "fr"
["toStringFormat":protected]=>
string(19) "yyyy-MM-dd HH:mm:ss"
["date"]=>
string(26) "2019-02-08 12:27:42.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(12) "Europe/Paris"
}
Good things thats work as expected.
print_r( $data )
Code:
Array
(
[success] => 1
[message] =>
[data] => Array
(
[0] => App\Entities\User Object
(
[id:protected] => 1
[firstname:protected] => Darth
[lastname:protected] => Vader
[username:protected] =>
[email:protected] => [email protected]
[password:protected] => 5e884898da28047151d0e56f8dc6292
[created_at:protected] => 2019-02-08 12:27:42
[updated_at:protected] =>
[deleted:protected] => 0
[deleted_at:protected] =>
[_options:protected] => Array
(
[datamap] => Array
(
)
[dates] => Array
(
[0] => created_at
[1] => updated_at
[2] => deleted_at
)
[casts] => Array
(
[deleted] => boolean
)
)
[_original:protected] => Array
(
[id] => 1
[firstname] => Darth
[lastname] => Vader
[username] =>
[email] => [email protected]
[password] => 5e884898da28047151d0e56f8dc6292
[created_at] => 2019-02-08 12:27:42
[updated_at] =>
[deleted] => 0
[deleted_at] =>
)
[_cast:CodeIgniter\Entity:private] => 1
)
)
)
but if i do
return $this->response->setStatusCode(200)->setJSON($data);
Code:
{
"success": true,
"message": "",
"data": [
{}
]
}
Data is totally empty. in fact, the problem is not a relation between entities and model. it's more generic... Like : "How to retrieve a correct formated data ?"
here is my controller/model/entity
Controllers/Users.php
PHP Code:
<?php namespace App\Controllers;
use CodeIgniter\Controller;
use CodeIgniter\API\ResponseTrait;
use App\Models\UserModel;
use App\Entities\User;
class Users extends Controller
{
public function index()
{
$userModel = new UserModel();
$users_data = $userModel->findAll();
if ($users_data) {
$data = [
'success' => true,
'message' => '',
'data' => $users_data,
];
echo '<pre>';
print_r($data);
// var_dump($data['data'][0]->id);
echo '</pre>';
// return $this->response->setStatusCode(200)->setJSON($data);
}
else
{
$data = [
'success' => false,
'message' => 'not_found',
'data' => [],
];
return $this->response->setStatusCode(404)->setJSON($data);
}
}
//--------------------------------------------------------------------
// End of file Users.php
//--------------------------------------------------------------------
}
Entities/User.php
PHP Code:
<?php namespace App\Entities;
use CodeIgniter\Entity;
class User extends Entity
{
protected $id;
protected $firstname;
protected $lastname;
protected $username;
protected $email;
protected $password;
protected $created_at;
protected $updated_at;
protected $deleted;
protected $deleted_at;
protected $_options = [
'datamap' => [],
'dates' => [
'created_at',
'updated_at',
'deleted_at'
],
'casts' => [
'deleted' => 'boolean'
],
];
}
Models/UserModel.php
PHP Code:
<?php namespace App\Models;
use CodeIgniter\Model;
use App\Entities\User;
class UserModel extends Model
{
/**
* Model Configuration
*/
protected $table = 'users';
protected $primaryKey = 'id';
protected $returnType = User::class;
protected $allowedFields = [
'id',
'firstname',
'lastname',
'username',
'email',
'password',
'created_at',
'updated_at',
'deleted',
'deleted_at',
];
protected $useSoftDeletes = true;
protected $dateFormat = 'datetime';
protected $skipValidation = false;
}