Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 34 additions & 9 deletions source/devapi/database/dbupdate.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@ Escaping of data is currently provided automatically by the framework for all da

The `WHERE` part of `UPDATE` and `DELETE` methods uses the same :ref:`criteria capabilities <query_criteria>` than `SELECT` queries.

Inserting a row
^^^^^^^^^^^^^^^
Inserting rows
^^^^^^^^^^^^^^

You can insert a row in the database using the ``insert()`` method.

An ``insertOrDie()`` method is also provided.

You can insert a row in the database using the ``insert()``:
Insert from values
""""""""""""""""""

You can insert a new row using fixed values for each column:

.. code-block:: php

Expand All @@ -29,10 +36,28 @@ You can insert a row in the database using the ``insert()``:
);
// => INSERT INTO `glpi_my_table` (`a_field`, `other_field`) VALUES ('My value', Other value)

An ``insertOrDie()`` method is also provided.
Insert from a selection
"""""""""""""""""""""""

Updating a row
^^^^^^^^^^^^^^
You can also insert new rows from a selection using :ref:`sub_queries`:

.. code-block:: php

<?php

$DB->insert(
'glpi_my_table', new \QuerySubQuery([
'SELECT' => [
'a_field',
'other_field'
],
'FROM' => 'glpi_my_other_table'
])
);
// => INSERT INTO `glpi_my_table` (SELECT `a_field`, `other_field` FROM `glpi_my_other_table`)

Updating rows
^^^^^^^^^^^^^

You can update rows in the database using the ``update()`` method:

Expand Down Expand Up @@ -69,8 +94,8 @@ When issuing an `UPDATE` query, you can use an `ORDER` and/or a `LIMIT` clause a
]
);

Removing a row
^^^^^^^^^^^^^^
Removing rows
^^^^^^^^^^^^^

You can remove rows from the database using the ``delete()`` method:

Expand Down Expand Up @@ -105,7 +130,7 @@ Let's see an example with an insert statement:
$stmt = $DB->prepare($insert_query);

foreach ($data as $row) {
$stmt->bind_params(
$stmt->bind_param(
'ss',
$row['field'],
$row['other']
Expand Down