Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: BaseModel::insert() may not pass all the values from Entity #4980

Merged
merged 8 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -1578,7 +1578,7 @@ protected function transformDataToArray($data, string $type): array
// properties representing the collection elements, we need to grab
// them as an array.
if (is_object($data) && ! $data instanceof stdClass) {
$data = $this->objectToArray($data, true, true);
$data = $this->objectToArray($data, ($type === 'update'), true);
paulbalandan marked this conversation as resolved.
Show resolved Hide resolved
}

// If it's still a stdClass, go ahead and convert to
Expand Down
41 changes: 41 additions & 0 deletions tests/_support/Entity/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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;

class User extends Entity
{
/**
* Maps names used in sets and gets against unique
* names within the class, allowing independence from
* database column names.
*
* Example:
* $datamap = [
* 'db_name' => 'class_name'
* ];
*/
protected $datamap = [];

/**
* Define properties that are automatically converted to Time instances.
*/
// protected $dates = ['created_at', 'updated_at', 'deleted_at', 'expiry'];
kenjis marked this conversation as resolved.
Show resolved Hide resolved
protected $dates = ['created_at', 'expiry'];

/**
* Array of field names and the type of value to cast them as
* when they are accessed.
*/
protected $casts = [];
}
31 changes: 31 additions & 0 deletions tests/_support/Models/UserObjModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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';
public $name = '';
public $email = '';
public $country = '';
kenjis marked this conversation as resolved.
Show resolved Hide resolved
}
27 changes: 27 additions & 0 deletions tests/system/Models/InsertModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -286,4 +288,29 @@ public function testInsertWithSetAndEscape(): void

$this->assertCloseEnough(time(), strtotime($result->created_at));
}

public function testInsertWithUpdate(): void
{
$entity = new User();
$this->createModel(UserObjModel::class);

$entity->name = 'Mark';
$entity->email = '[email protected]';
$entity->country = 'India';
$entity->deleted = 0;
$entity->created_at = new Time('now');

$this->model->insert($entity);
$this->assertGreaterThan(0, $this->model->getInsertID());
$resultEntity = $this->model->where('name', 'Mark')->first();

$resultEntity->name = 'Katherine';

$this->model->insert($resultEntity);
$this->assertGreaterThan(0, $this->model->getInsertID());

$resultEntity2 = $this->model->where('name', 'Katherine')->first();
$this->assertGreaterThan(0, $this->model->getInsertID());
$this->assertSame('[email protected]', $resultEntity2->email);
}
kenjis marked this conversation as resolved.
Show resolved Hide resolved
katie1348 marked this conversation as resolved.
Show resolved Hide resolved
}