Skip to content

Commit bc02c21

Browse files
committed
Add delete from query documentation
1 parent 055c580 commit bc02c21

3 files changed

Lines changed: 32 additions & 1 deletion

File tree

tests/system/Database/Live/DeleteTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace CodeIgniter\Database\Live;
1313

1414
use CodeIgniter\Database\Exceptions\DatabaseException;
15+
use CodeIgniter\Database\Forge;
1516
use CodeIgniter\Test\CIUnitTestCase;
1617
use CodeIgniter\Test\DatabaseTestTrait;
1718
use Config\Database;
@@ -26,6 +27,11 @@ final class DeleteTest extends CIUnitTestCase
2627
{
2728
use DatabaseTestTrait;
2829

30+
/**
31+
* @var Forge|mixed
32+
*/
33+
public $forge;
34+
2935
protected $refresh = true;
3036
protected $seed = CITestSeeder::class;
3137

@@ -145,7 +151,7 @@ public function testDeleteBatchWithQuery()
145151
$this->seeInDatabase('user', ['name' => 'Ahmadinejad', 'email' => 'ahmadinejad@world.com']);
146152
$this->dontSeeInDatabase('user', ['name' => 'Chris Martin', 'email' => 'chris@world.com']);
147153

148-
$result = $this->db->table('user')->get()->getResultArray();
154+
$this->db->table('user')->get()->getResultArray();
149155

150156
$this->forge->dropTable('user2', true);
151157
}

user_guide_src/source/database/query_builder.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,10 @@ This method may be especially useful when deleting data in a table with a compos
11401140

11411141
.. note:: SQLite does not support the use of ``where()``.
11421142

1143+
You can also delete from a query:
1144+
1145+
.. literalinclude:: query_builder/119.php
1146+
11431147
$builder->emptyTable()
11441148
----------------------
11451149

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use CodeIgniter\Database\RawSql;
4+
5+
$query = $this->db->table('user2')->select('email, name, country')->where('country', 'Greece');
6+
7+
$this->db->table('user')
8+
->setQueryAsData($query, 'alias')
9+
->onConstraint('email')
10+
->where('alias.name = user.name')
11+
->deleteBatch();
12+
13+
/* MySQLi produces:
14+
DELETE `user` FROM `user`
15+
INNER JOIN (
16+
SELECT `email`, `name`, `country`
17+
FROM `user2`
18+
WHERE `country` = 'Greece') `alias`
19+
ON `user`.`email` = `alias`.`email`
20+
WHERE `alias`.`name` = `user`.`name`
21+
*/

0 commit comments

Comments
 (0)