PHP Version
8.3
CodeIgniter4 Version
4.4.5
CodeIgniter4 Installation Method
Composer (using codeigniter4/appstarter)
Which operating systems have you tested for this bug?
Linux
Which server did you use?
cli-server (PHP built-in webserver)
Database
SQLite3
What happened?
In documentation about Entities in section Create Model is sentence Finally, we’ve set our Entity class as the $returnType. This ensures that all methods on the model that return rows from the database will return instances of our User Entity class instead of an object or array like normal.
It is assumed that the results always bee as set in $returnType for example protected $returnType = UserEntity::class;
Steps to Reproduce
Create database table users:
CREATE TABLE `users` (
id INTEGER PRIMARY KEY,
user_name TEXT NOT NULL,
user_full_name TEXT NOT NULL,
password TEXT NOT NULL,
email TEXT NOT NULL,
created_at TEXT,
updated_at TEXT,
deleted_at TEXT
);
Fill table with some random data to run queries.
Create controller, model and entity using php spark make:....
In UserEntity add few properties:
private $userName;
private $fullName;
private $password;
private $email;
In UserModel set protected $returnType = UserEntity::class;
In UserContoller create index to display results.
In Index function
public function index()
{
$uModel = new UserModel();
$user = $uModel->find(1);
d($user->userName); //Display user name
d($user->user_name); //Display user name
d($user->getUserName()); //Cause error: Call to undefined method App\Entities\UserEntity::getUserName()
}
In UserEntity create set/get method for [get/set]UserName and [get/set]FullName
private $userName;
private $fullName;
public function setUserName($name)
{
$this->userName = $name;
return $this;
}
public function getUserName() :?string
{
return $this->userName;
}
public function setFullName($name)
{
$this->fullName = $name;
return $this;
}
public function getFullName()
{
return $this->fullName;
}
Index method return:
d($user->user_name); // null
d($user->userName); // null
d($user->getUserName()); // null
In UserContoller swith from $user = $uModel->find(6); to $user = $uModel->find(1);
In UserModel create method:
public function getByID($id)
{
$q = $this->db->table($this->table)->where('id',$id)->get();
// Result example 1
//return $q->getRow();
// Result example 2
// return $q->getRow(0, UserEntity::class);
// Result example 3
// return $q->getCustomRowObject(0, UserEntity::class);
// Result example 4
// $r = $q->getRowArray();
// $u = new UserEntity();
// $u->fill($r);
// return $u;
}
Result example 1: Undefined property: stdClass::$userName
Result example 2: userName still null
Result example 3: userName still null
Result example 4:
d($user->user_name); // field is filled with user name
d($user->userName); // field is filled with user name
d($user->getUserName()); // field is filled with user name
When in controller call $user->fullName or $user->getFullName() result is NULL.
I added datamap as describer in doc
protected $datamap = [
// property_name => db_column_name
'userName' => 'user_name',
'fullName' => 'user_full_name',
'password' => 'password',
'email' => 'email',
];
Result was no changed. But when i change places key and value
protected $datamap = [
// db_column_name => property_name
'user_name' => 'userName',
'user_full_name' => 'fullName',
'password' => 'password',
'email' => 'email',
];
Then method $user->getFullName() in contoller return result as expected
Expected Output
Return UserEntity when returning result from model
Anything else?
No response
PHP Version
8.3
CodeIgniter4 Version
4.4.5
CodeIgniter4 Installation Method
Composer (using
codeigniter4/appstarter)Which operating systems have you tested for this bug?
Linux
Which server did you use?
cli-server (PHP built-in webserver)
Database
SQLite3
What happened?
In documentation about Entities in section
Create Modelis sentenceFinally, we’ve set our Entity class as the $returnType. This ensures that all methods on the model that return rows from the database will return instances of our User Entity class instead of an object or array like normal.It is assumed that the results always bee as set in
$returnTypefor exampleprotected $returnType = UserEntity::class;Steps to Reproduce
Create database table
users:Fill table with some random data to run queries.
Create controller, model and entity using
php spark make:....In
UserEntityadd few properties:In
UserModelsetprotected $returnType = UserEntity::class;In
UserContollercreateindexto display results.In Index function
In
UserEntitycreate set/get method for[get/set]UserNameand[get/set]FullNameIndexmethod return:In
UserContollerswith from$user = $uModel->find(6);to$user = $uModel->find(1);In
UserModelcreate method:Result example 1: Undefined property: stdClass::$userNameResult example 2: userName still nullResult example 3: userName still nullResult example 4:When in controller call
$user->fullNameor$user->getFullName()result isNULL.I added
datamapas describer in docResult was no changed. But when i change places
keyandvalueThen method
$user->getFullName()in contoller return result as expectedExpected Output
Return
UserEntitywhen returning result from modelAnything else?
No response