Child pages
  • DB class best practices

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
INSERT INTO `prefix_target_table` (`id_target`, `name`) VALUES (10, 'myName')
Info

Veillez à toujours protéger vos données avant de les passer à insert(). Dans l’exemple, on s’assure que id_target soit un entier et que name soit protégé contre les injections SQL avec pSQL()Make sure that your data is always checked and protected when doing an insertion. In our example, we want to make sure that we do have an integer with an explicit (int) cast, and that the name is protected against SQL injections thanks to the pSQL() method.

Method parameters

Parameter

Description

$table

Table's name. The PrestaShop prefix is automatically inserted, you do not have to put it in.

$data

The data array, containing the data to be inserted, with name as keys and data as values.

$null_values

If true, then values that are passed as NULL will be inserted as such in the database.

$use_cache

If false, PrestaShop's cache management is disabled during this query. Do not change this parameter unless you knew exactly what you are doing.

$type

If you wish to change the insertion, this parameter can take the following constants: Db::INSERT, Db::INSERT_IGNORE or Db::REPLACE.

$add_prefix

If flase false, table prefix will not be automatically added to the table name.

update()

Methode Method signature: update($table, $data, $where = '', $limit = 0, $null_values = false, $use_cache = true, $add_prefix = true) ($table, $data, $null_values = false, $use_cache = true, $type = Db::INSERT, $add_prefix = true)

This method works as the insert() method does, but for data update (UPDATE queries). Both have roughly the same parameters, with type gone and these two additions:

Parameter

Description

$where

Takes the update's WHERE clause.

$limit

You can limit the number of results records that you will update.

...

delete()

Method signature: delete($table, $where = '', $limit = 0, $use_cache = true, $add_prefix = true)

...

Cette méthode est l’équivalent de .

This method is an equivalent to insert() et and update() en version , only for DELETE. Elle est à utiliser pour les mêmes raisons. L’argument $limit permet de limiter le nombre d’enregistrements que l’on souhaite supprimer. L’autre avantage d’utiliser cette méthode est qu’elle sera utilisée par le système de cache des requêtes SQL de PrestaShop et effacera donc les requêtes concernées en cache sauf si l’argument $use_cache vaut false.Exemple queries. You should use it for the same reasons.

The $limit parameter enables you to limit the number of records to that you wish to delete. The other advantage of this method is that it will be used by PrestaShop's SQL queries cache system, and will therefore delete the affected queries in cache, unles the $use_cache is false.

Example:

Code Block
Db::getInstance()->delete(‘target_table’, ‘myField < 15’, 3);

va générer la requête...will generate the following query:

Code Block
DELETE FROM `prefix_target_table` WHERE myField < 15 LIMIT 3

...

execute()

Method signature: execute($sql, $use_cache = 1)

This method executes the given SQL query.

Cette méthode exécute la requête SQL donnée. Elle n’est à utiliser que pour les requêtes en écriture (INSERT, UPDATE, DELETE, TRUNCATE…) car elle supprime de plus le cache de requêtes (sauf si l’argument $use_cache vaut false).

...