Skip to content

Commit e5be1f1

Browse files
authored
Merge pull request #1981 from codeigniter4/softdelete
Using soft deletes should not return an ambiguous field message when joining tables. Closes #1881
2 parents b56dac9 + f07188b commit e5be1f1

2 files changed

Lines changed: 85 additions & 39 deletions

File tree

system/Model.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ public function find($id = null)
322322

323323
if ($this->tempUseSoftDeletes === true)
324324
{
325-
$builder->where($this->deletedField, 0);
325+
$builder->where($this->table . '.' . $this->deletedField, 0);
326326
}
327327

328328
if (is_array($id))
@@ -361,7 +361,6 @@ public function find($id = null)
361361
* @param string $columnName
362362
*
363363
* @return array|null The resulting row of data, or null if no data found.
364-
*
365364
*/
366365
public function findColumn(string $columnName)
367366
{
@@ -371,10 +370,10 @@ public function findColumn(string $columnName)
371370
}
372371

373372
$resultSet = $this->select($columnName)
374-
->asArray()
375-
->find();
373+
->asArray()
374+
->find();
376375

377-
return (!empty($resultSet)) ? array_column($resultSet, $columnName) : null;
376+
return (! empty($resultSet)) ? array_column($resultSet, $columnName) : null;
378377
}
379378

380379
//--------------------------------------------------------------------
@@ -394,7 +393,7 @@ public function findAll(int $limit = 0, int $offset = 0)
394393

395394
if ($this->tempUseSoftDeletes === true)
396395
{
397-
$builder->where($this->deletedField, 0);
396+
$builder->where($this->table . '.' . $this->deletedField, 0);
398397
}
399398

400399
$row = $builder->limit($limit, $offset)
@@ -424,7 +423,7 @@ public function first()
424423

425424
if ($this->tempUseSoftDeletes === true)
426425
{
427-
$builder->where($this->deletedField, 0);
426+
$builder->where($this->table . '.' . $this->deletedField, 0);
428427
}
429428

430429
// Some databases, like PostgreSQL, need order

tests/system/Database/Live/ModelTest.php

Lines changed: 79 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ public function testSaveNewRecordArray()
324324
];
325325

326326
$model->protect(false)
327-
->save($data);
327+
->save($data);
328328

329329
$this->seeInDatabase('job', ['name' => 'Apprentice']);
330330
}
@@ -736,8 +736,8 @@ public function testCanCreateAndSaveEntityClasses()
736736
$this->assertTrue($model->save($entity));
737737

738738
$result = $model->where('name', 'Senior Developer')
739-
->get()
740-
->getFirstRow();
739+
->get()
740+
->getFirstRow();
741741

742742
$this->assertEquals(date('Y-m-d', $time), date('Y-m-d', $result->created_at));
743743
}
@@ -1033,8 +1033,8 @@ public function testSelectAndEntitiesSaveOnlyChangedValues()
10331033

10341034
// get only id, name column
10351035
$job = $model->select('id, name')
1036-
->where('name', 'Rocket Scientist')
1037-
->first();
1036+
->where('name', 'Rocket Scientist')
1037+
->first();
10381038

10391039
// Hence getting Null as description column not in select clause
10401040
$this->assertNull($job->description);
@@ -1055,8 +1055,8 @@ public function testSelectAndEntitiesSaveOnlyChangedValues()
10551055

10561056
// select all columns from job table
10571057
$job = $model->select('id, name, description')
1058-
->where('name', 'Rocket Scientist')
1059-
->first();
1058+
->where('name', 'Rocket Scientist')
1059+
->first();
10601060

10611061
// check whether the Null value successfully updated or not
10621062
$this->assertEquals('Some guitar description', $job->description);
@@ -1234,7 +1234,7 @@ public function testInsertID()
12341234
];
12351235

12361236
$model->protect(false)
1237-
->save($data);
1237+
->save($data);
12381238

12391239
$lastInsertId = $model->getInsertID();
12401240

@@ -1255,7 +1255,7 @@ public function testSetTable()
12551255
];
12561256

12571257
$model->protect(false)
1258-
->save($data);
1258+
->save($data);
12591259

12601260
$lastInsertId = $model->getInsertID();
12611261

@@ -1286,8 +1286,8 @@ public function testValidationByObject()
12861286
public $token = '';
12871287
};
12881288

1289-
$data->name = 'abc';
1290-
$data->id = '13';
1289+
$data->name = 'abc';
1290+
$data->id = '13';
12911291
$data->token = '13';
12921292

12931293
$this->assertTrue($model->validate($data));
@@ -1348,7 +1348,7 @@ public function testSaveObject()
13481348

13491349
$testModel = new JobModel();
13501350

1351-
$testModel->name = 'my name';
1351+
$testModel->name = 'my name';
13521352
$testModel->description = 'some description';
13531353

13541354
$this->setPrivateProperty($model, 'useTimestamps', true);
@@ -1361,15 +1361,15 @@ public function testSaveObject()
13611361
}
13621362

13631363
//--------------------------------------------------------------------
1364-
1364+
13651365
public function testEmptySaveData()
13661366
{
13671367
$model = new JobModel();
13681368

13691369
$data = [];
13701370

13711371
$data = $model->protect(false)
1372-
->save($data);
1372+
->save($data);
13731373

13741374
$this->assertTrue($data);
13751375
}
@@ -1382,7 +1382,7 @@ public function testUpdateObject()
13821382

13831383
$testModel = new JobModel();
13841384

1385-
$testModel->name = 'my name';
1385+
$testModel->name = 'my name';
13861386
$testModel->description = 'some description';
13871387

13881388
$this->setPrivateProperty($model, 'useTimestamps', true);
@@ -1413,8 +1413,8 @@ public function testPurgeDeletedWithSoftDeleteFalse()
14131413
$model = new JobModel();
14141414

14151415
$this->db->table('job')
1416-
->where('id', 1)
1417-
->update(['deleted' => 1]);
1416+
->where('id', 1)
1417+
->update(['deleted' => 1]);
14181418

14191419
$model->purgeDeleted();
14201420

@@ -1463,7 +1463,7 @@ public function testGetValidationMessagesForReplace()
14631463

14641464
public function testSaveNewEntityWithDateTime()
14651465
{
1466-
$entity = new class extends Entity{
1466+
$entity = new class extends Entity{
14671467
protected $id;
14681468
protected $name;
14691469
protected $email;
@@ -1484,10 +1484,10 @@ public function testSaveNewEntityWithDateTime()
14841484
};
14851485
$testModel = new UserModel();
14861486

1487-
$entity->name = 'Mark';
1488-
$entity->email = 'mark@example.com';
1489-
$entity->country = 'India';
1490-
$entity->deleted = 0;
1487+
$entity->name = 'Mark';
1488+
$entity->email = 'mark@example.com';
1489+
$entity->country = 'India';
1490+
$entity->deleted = 0;
14911491
$entity->created_at = new Time('now');
14921492

14931493
$this->setPrivateProperty($testModel, 'useTimestamps', true);
@@ -1499,7 +1499,7 @@ public function testSaveNewEntityWithDateTime()
14991499

15001500
public function testSaveNewEntityWithDate()
15011501
{
1502-
$entity = new class extends Entity
1502+
$entity = new class extends Entity
15031503
{
15041504
protected $id;
15051505
protected $name;
@@ -1517,17 +1517,17 @@ public function testSaveNewEntityWithDate()
15171517
};
15181518
$testModel = new class extends Model
15191519
{
1520-
protected $table = 'empty';
1521-
protected $allowedFields = [
1520+
protected $table = 'empty';
1521+
protected $allowedFields = [
15221522
'name',
15231523
];
1524-
protected $returnType = 'object';
1524+
protected $returnType = 'object';
15251525
protected $useSoftDeletes = true;
1526-
protected $dateFormat = 'date';
1527-
public $name = '';
1526+
protected $dateFormat = 'date';
1527+
public $name = '';
15281528
};
15291529

1530-
$entity->name = 'Mark';
1530+
$entity->name = 'Mark';
15311531
$entity->created_at = new Time('now');
15321532

15331533
$this->setPrivateProperty($testModel, 'useTimestamps', true);
@@ -1556,7 +1556,7 @@ public function testUndefinedEntityPropertyException()
15561556
public function testInsertWithNoDataException()
15571557
{
15581558
$model = new UserModel();
1559-
$data = [];
1559+
$data = [];
15601560
$this->expectException(DataException::class);
15611561
$this->expectExceptionMessage('There is no data to insert.');
15621562
$model->insert($data);
@@ -1596,7 +1596,7 @@ public function testInvalidAllowedFieldException()
15961596
'description' => 'That thing you do.',
15971597
];
15981598

1599-
$this->setPrivateProperty($model,'allowedFields', []);
1599+
$this->setPrivateProperty($model, 'allowedFields', []);
16001600

16011601
$this->expectException(DataException::class);
16021602
$this->expectExceptionMessage('Allowed fields must be specified for model: Tests\Support\Models\JobModel');
@@ -1617,7 +1617,7 @@ public function testInvalidEventException()
16171617
'deleted' => 0,
16181618
];
16191619

1620-
$this->setPrivateProperty($model,'beforeInsert',['anotherBeforeInsertMethod']);
1620+
$this->setPrivateProperty($model, 'beforeInsert', ['anotherBeforeInsertMethod']);
16211621

16221622
$this->expectException(DataException::class);
16231623
$this->expectExceptionMessage('anotherBeforeInsertMethod is not a valid Model Event callback.');
@@ -1627,4 +1627,51 @@ public function testInvalidEventException()
16271627

16281628
//--------------------------------------------------------------------
16291629

1630+
/**
1631+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/1881
1632+
*/
1633+
public function testSoftDeleteWithTableJoinsFindAll()
1634+
{
1635+
$model = new UserModel();
1636+
1637+
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted' => 0]);
1638+
1639+
$results = $model->join('job', 'job.id = user.id')
1640+
->findAll();
1641+
1642+
// Just making sure it didn't throw ambiguous delete error
1643+
$this->assertCount(4, $results);
1644+
}
1645+
1646+
/**
1647+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/1881
1648+
*/
1649+
public function testSoftDeleteWithTableJoinsFind()
1650+
{
1651+
$model = new UserModel();
1652+
1653+
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted' => 0]);
1654+
1655+
$results = $model->join('job', 'job.id = user.id')
1656+
->find(1);
1657+
1658+
// Just making sure it didn't throw ambiguous deleted error
1659+
$this->assertEquals(1, $results->id);
1660+
}
1661+
1662+
/**
1663+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/1881
1664+
*/
1665+
public function testSoftDeleteWithTableJoinsFirst()
1666+
{
1667+
$model = new UserModel();
1668+
1669+
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted' => 0]);
1670+
1671+
$results = $model->join('job', 'job.id = user.id')
1672+
->first(1);
1673+
1674+
// Just making sure it didn't throw ambiguous deleted error
1675+
$this->assertEquals(1, $results->id);
1676+
}
16301677
}

0 commit comments

Comments
 (0)