-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: BaseModel::insert() may not pass all the values from Entity (#4980)
* Update of BaseModel.php to fix INSERT issue Applied the fix that enables the INSERT to work correctly when inserting a row that already contains data. * Update BaseModel.php * Added tests for fix. User.php is an Entity UserObjModel.php returns an entity not StdObj * Updated to simplify usings. * Updated to cleanup test code. * Update tests/system/Models/InsertModelTest.php Co-authored-by: kenjis <[email protected]> * Update tests/_support/Entity/User.php Co-authored-by: kenjis <[email protected]> Co-authored-by: Katherine <[email protected]> Co-authored-by: kenjis <[email protected]>
- Loading branch information
1 parent
d1515aa
commit b8fe16d
Showing
4 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of CodeIgniter 4 framework. | ||
* | ||
* (c) CodeIgniter Foundation <[email protected]> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Tests\Support\Entity; | ||
|
||
use CodeIgniter\Entity\Entity; | ||
|
||
class User extends Entity | ||
{ | ||
protected $attributes = [ | ||
'country' => 'India', | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of CodeIgniter 4 framework. | ||
* | ||
* (c) CodeIgniter Foundation <[email protected]> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Tests\Support\Models; | ||
|
||
use CodeIgniter\Model; | ||
|
||
class UserObjModel extends Model | ||
{ | ||
protected $table = 'user'; | ||
protected $allowedFields = [ | ||
'name', | ||
'email', | ||
'country', | ||
'deleted_at', | ||
]; | ||
protected $returnType = \Tests\Support\Entity\User::class; | ||
protected $useSoftDeletes = true; | ||
protected $dateFormat = 'datetime'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,8 +15,10 @@ | |
use CodeIgniter\Entity\Entity; | ||
use CodeIgniter\I18n\Time; | ||
use stdClass; | ||
use Tests\Support\Entity\User; | ||
use Tests\Support\Models\JobModel; | ||
use Tests\Support\Models\UserModel; | ||
use Tests\Support\Models\UserObjModel; | ||
use Tests\Support\Models\WithoutAutoIncrementModel; | ||
|
||
/** | ||
|
@@ -286,4 +288,24 @@ public function testInsertWithSetAndEscape(): void | |
|
||
$this->assertCloseEnough(time(), strtotime($result->created_at)); | ||
} | ||
|
||
/** | ||
* @see https://github.com/codeigniter4/CodeIgniter4/issues/4247 | ||
*/ | ||
public function testInsertWithDefaultValue(): void | ||
{ | ||
$this->createModel(UserObjModel::class); | ||
|
||
$entity = new User(); | ||
$entity->name = 'Mark'; | ||
$entity->email = '[email protected]'; | ||
$entity->country = 'India'; // same as the default | ||
$entity->deleted = 0; | ||
$entity->created_at = new Time('now'); | ||
|
||
$this->model->insert($entity); | ||
|
||
$id = $this->model->getInsertID(); | ||
$this->assertSame($entity->country, $this->model->find($id)->country); | ||
} | ||
} |