You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/datamapper.md
+136-35
Original file line number
Diff line number
Diff line change
@@ -626,7 +626,9 @@ The parameters available are:
626
626
The parameters must be enclosed in curly brackets `{}`
627
627
628
628
## Query
629
+
629
630
### Factory
631
+
630
632
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.
631
633
632
634
#### Methods
@@ -661,6 +663,7 @@ public function newUpdate(Connection $connection): Update
Processes a value (array or string) and merges it with the store
925
928
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].
926
931
932
+
```php
933
+
<?php
927
934
935
+
use Phalcon\DataMapper\Pdo\Connection;
936
+
use Phalcon\DataMapper\Query\QueryFactory;
928
937
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';
933
944
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
+
);
937
952
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
+
```
939
957
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
948
959
949
-
// DELETE ... ORDER BY foo, bar, baz
960
+
The `from()` method is used to specify the table to delete data from.
961
+
962
+
```php
950
963
$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
+
;
955
966
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
957
995
$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
962
1010
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
964
1014
$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
969
1032
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
971
1036
$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.
0 commit comments