Skip to content

Commit

Permalink
Added ability to string primary key via Model::delete($id)
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Apr 27, 2020
1 parent 9355f03 commit 6e59f9c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
6 changes: 3 additions & 3 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -898,15 +898,15 @@ public function updateBatch(array $set = null, string $index = null, int $batchS
* Deletes a single record from $this->table where $id matches
* the table's primaryKey
*
* @param integer|array|null $id The rows primary key(s)
* @param boolean $purge Allows overriding the soft deletes setting.
* @param integer|string|array|null $id The rows primary key(s)
* @param boolean $purge Allows overriding the soft deletes setting.
*
* @return mixed
* @throws \CodeIgniter\Database\Exceptions\DatabaseException
*/
public function delete($id = null, bool $purge = false)
{
if (! empty($id) && is_numeric($id))
if (! empty($id) && (is_numeric($id) || is_string($id)))
{
$id = [$id];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ public function up()
]);
$this->forge->addKey('id', true);
$this->forge->createTable('secondary', true);

// Stringify Primary key Table
$this->forge->addField([
'id' => [
'type' => 'VARCHAR',
'constraint' => 3,
],
'value' => ['type' => 'TEXT'],
]);
$this->forge->addKey('id', true);
$this->forge->createTable('stringifypkey', true);
}

//--------------------------------------------------------------------
Expand All @@ -141,6 +152,7 @@ public function down()
$this->forge->dropTable('misc', true);
$this->forge->dropTable('empty', true);
$this->forge->dropTable('secondary', true);
$this->forge->dropTable('stringifypkey', true);
}

//--------------------------------------------------------------------
Expand Down
12 changes: 9 additions & 3 deletions tests/_support/Database/Seeds/CITestSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public function run()
{
// Job Data
$data = [
'user' => [
'user' => [
[
'name' => 'Derek Jones',
'email' => '[email protected]',
Expand All @@ -28,7 +28,7 @@ public function run()
'country' => 'UK',
],
],
'job' => [
'job' => [
[
'name' => 'Developer',
'description' => 'Awesome job, but sometimes makes you bored',
Expand All @@ -46,7 +46,7 @@ public function run()
'description' => 'Only Coldplay can actually called Musician',
],
],
'misc' => [
'misc' => [
[
'key' => '\\xxxfoo456',
'value' => 'Entry with \\xxx',
Expand All @@ -60,6 +60,12 @@ public function run()
'value' => ' One two three tab',
],
],
'stringifypkey' => [
[
'id' => 'A01',
'value' => 'test',
],
],
];

foreach ($data as $table => $dummy_data)
Expand Down
9 changes: 9 additions & 0 deletions tests/_support/Models/StringifyPkeyModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php namespace Tests\Support\Models;

use CodeIgniter\Model;

class StringifyPkeyModel extends Model
{
protected $table = 'stringifypkey';
protected $returnType = 'object';
}
14 changes: 14 additions & 0 deletions tests/system/Database/Live/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Tests\Support\Models\JobModel;
use Tests\Support\Models\SecondaryModel;
use Tests\Support\Models\SimpleEntity;
use Tests\Support\Models\StringifyPkeyModel;
use Tests\Support\Models\UserModel;
use Tests\Support\Models\ValidErrorsModel;
use Tests\Support\Models\ValidModel;
Expand Down Expand Up @@ -399,6 +400,19 @@ public function testDeleteBasics()

//--------------------------------------------------------------------

public function testDeleteStringPrimaryKey()
{
$model = new StringifyPkeyModel();

$this->seeInDatabase('stringifypkey', ['value' => 'test']);

$model->delete('A01');

$this->dontSeeInDatabase('stringifypkey', ['value' => 'test']);
}

//--------------------------------------------------------------------

public function testDeleteWithSoftDeletes()
{
$model = new UserModel();
Expand Down

0 comments on commit 6e59f9c

Please sign in to comment.