Skip to content

Commit 20662b4

Browse files
committed
reformatting for delete
1 parent 36b807d commit 20662b4

File tree

1 file changed

+136
-35
lines changed

1 file changed

+136
-35
lines changed

Diff for: docs/datamapper.md

+136-35
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,9 @@ The parameters available are:
626626
The parameters must be enclosed in curly brackets `{}`
627627

628628
## Query
629+
629630
### Factory
631+
630632
The `Phalcon\DataMapper\Query` namespace offers a handy factory, which allows for a quick and easy creation of query objects, whether this is `select`, `insert`, `update` or `delete. The methods exposed by the [Phalcon\DataMapper\Query\QueryFactory][datamapper-query-queryfactory] accept a [Phalcon\DataMapper\Pdo\Connection][datamapper-pdo-connection], binding the resulting object with the connection.
631633

632634
#### Methods
@@ -661,6 +663,7 @@ public function newUpdate(Connection $connection): Update
661663
```
662664
Create a new Update object
663665

666+
#### Example
664667

665668
```php
666669
<?php
@@ -923,58 +926,156 @@ protected function processValue(string $store, mixed $data): void
923926
```
924927
Processes a value (array or string) and merges it with the store
925928

929+
#### Activation
930+
To instantiate a [Phalcon\DataMapper\Query\Delete][datamapper-query-delete] builder, you can use the [Phalcon\DataMapper\Query\QueryFactory][datamapper-query-queryfactory] with a [Phalcon\DataMapper\Pdo\Connection][datamapper-pdo-connection].
926931

932+
```php
933+
<?php
927934

935+
use Phalcon\DataMapper\Pdo\Connection;
936+
use Phalcon\DataMapper\Query\QueryFactory;
928937

929-
1.2.7. DELETE
930-
1.2.7.1. Building The Statement
931-
1.2.7.1.1. FROM
932-
Use the from() method to specify FROM expression.
938+
$host = '127.0.0.1';
939+
$database = 'phalon_test';
940+
$charset = 'utf8mb4';
941+
$port = 3306;
942+
$username = 'phalcon';
943+
$password = 'secret';
933944

934-
$delete->from('foo');
935-
1.2.7.1.2. WHERE
936-
(All WHERE methods support implicit and sprintf() inline value binding.)
945+
$dsn = sprintf(
946+
"mysql:host=%s;dbname=%s;charset=%s;port=%s",
947+
$host,
948+
$database,
949+
$charset,
950+
$port
951+
);
937952

938-
The Delete WHERE methods work just like their equivalent Select methods:
953+
$connection = new Connection($dsn, $username, $password);
954+
$factory = new QueryFactory();
955+
$delete = $factory->newDelete($connection);
956+
```
939957

940-
where() and andWhere() AND a WHERE condition
941-
orWhere() ORs a WHERE condition
942-
catWhere() concatenates onto the end of the most-recent WHERE condition
943-
whereSprintf() and andWhereSprintf() AND a WHERE condition with sprintf()
944-
orWhereSprintf() ORs a WHERE condition with sprintf()
945-
catWhereSprintf() concatenates onto the end of the most-recent WHERE condition with sprintf()
946-
1.2.7.1.3. ORDER BY
947-
Some databases (notably MySQL) recognize an ORDER BY clause. You can add one to the Delete with the orderBy() method; pass each expression as a variadic argument.
958+
#### Build
948959

949-
// DELETE ... ORDER BY foo, bar, baz
960+
The `from()` method is used to specify the table to delete data from.
961+
962+
```php
950963
$delete
951-
->orderBy('foo')
952-
->orderBy('bar', 'baz');
953-
1.2.7.1.4. LIMIT and OFFSET
954-
Some databases (notably MySQL and SQLite) recognize a LIMIT clause; others (notably SQLite) recognize an additional OFFSET. You can add these to the Delete with the limit() and offset() methods:
964+
->from('co_invoices')
965+
;
955966

956-
// LIMIT 10 OFFSET 40
967+
$delete->perform();
968+
// DELETE
969+
// FROM co_invoices
970+
```
971+
972+
##### WHERE
973+
974+
The `where()` method(s) are used to specify conditions for the `DELETE` statement.
975+
976+
```php
977+
$delete
978+
->from('co_invoices')
979+
->where('inv_cst_id = ', 1)
980+
;
981+
982+
$delete->perform();
983+
984+
// DELETE
985+
// FROM co_invoices
986+
// WHERE inv_cst_id = 1
987+
988+
```
989+
990+
##### ORDER BY
991+
992+
Certain databases (in particular MySQL) accept `ORDER BY` on a delete. You can use the `orderBy()` to specify it.
993+
994+
```php
957995
$delete
958-
->limit(10)
959-
->offset(40);
960-
1.2.7.1.5. RETURNING
961-
Some databases (notably PostgreSQL) recognize a RETURNING clause. You can add one to the Delete using the returning() method, specifying columns as variadic arguments.
996+
->from('co_invoices')
997+
->where('inv_cst_id = ', 1)
998+
->orderBy('inv_id')
999+
;
1000+
1001+
$delete->perform();
1002+
1003+
// DELETE
1004+
// FROM co_invoices
1005+
// WHERE inv_cst_id = 1
1006+
// ORDER BY inv_id
1007+
```
1008+
1009+
##### LIMIT/OFFSET
9621010

963-
// DELETE ... RETURNING foo, bar, baz
1011+
Certain databases (MySQL, SQLite) accept a `LIMIT` and/or `OFFSET` clause. You can use the `limit()` and `offset()` methods to specify them.
1012+
1013+
```php
9641014
$delete
965-
->returning('foo')
966-
->returning('bar', 'baz');
967-
1.2.7.1.6. Flags
968-
You can set flags recognized by your database server using the setFlag() method. For example, you can set a MySQL LOW_PRIORITY flag like so:
1015+
->from('co_invoices')
1016+
->where('inv_cst_id = ', 1)
1017+
->orderBy('inv_id')
1018+
->limit(10)
1019+
->offset(40)
1020+
;
1021+
1022+
$delete->perform();
1023+
1024+
// DELETE
1025+
// FROM co_invoices
1026+
// WHERE inv_cst_id = 1
1027+
// ORDER BY inv_id
1028+
// LIMIT 10 OFFSET 40
1029+
```
1030+
1031+
##### RETURNING
9691032

970-
// DELETE LOW_PRIORITY foo WHERE baz = :_1_1_
1033+
Some databases (notably PostgreSQL) accept a `RETURNING` clause. You can use the `returning()` method to specify it.
1034+
1035+
```php
9711036
$delete
972-
->from('foo')
973-
->where('baz = ', $baz_value)
974-
->setFlag('LOW_PRIORITY');
1037+
->from('co_invoices')
1038+
->where('inv_cst_id = ', 1)
1039+
->orderBy('inv_id')
1040+
->limit(10)
1041+
->offset(40)
1042+
->returning(['inv_id', 'inv_cst_id'])
1043+
;
1044+
1045+
$delete->perform();
1046+
1047+
// DELETE
1048+
// FROM co_invoices
1049+
// WHERE inv_cst_id = 1
1050+
// ORDER BY inv_id
1051+
// LIMIT 10 OFFSET 40
1052+
// RETURNING inv_id, inv_cst_id
1053+
```
1054+
1055+
##### Flags
1056+
1057+
Certain databases also accept specific flags. Those can be set using the `setFlags()` method.
9751058

1059+
```php
1060+
$delete
1061+
->from('co_invoices')
1062+
->where('inv_cst_id = ', 1)
1063+
->orderBy('inv_id')
1064+
->limit(10)
1065+
->offset(40)
1066+
->returning(['inv_id', 'inv_cst_id'])
1067+
->setFlag('LOW_PRIORITY')
1068+
;
9761069

1070+
$delete->perform();
9771071

1072+
// DELETE LOW_PRIORITY
1073+
// FROM co_invoices
1074+
// WHERE inv_cst_id = 1
1075+
// ORDER BY inv_id
1076+
// LIMIT 10 OFFSET 40
1077+
// RETURNING inv_id, inv_cst_id
1078+
```
9781079

9791080

9801081
### Insert

0 commit comments

Comments
 (0)