Child pages
  • DB class best practices

Versions Compared

Key

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

Table of content

Table of Contents
maxLevel3
styledisc
printablefalse

DB class best practices

Most of the time, creating a module or overriding PrestaShop means using or inserting data in the database. Knowing how to properly use the DB core class is therefore mandatory for developers. Besides providing you with an abstraction for other potential database system, the DB class offers several tools to make your life easier.

...

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, table prefix will not be automatically added to the table name.

...

update()

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

Cette méthode s’utilise de la même façon que la méthode insert(), mais pour les mises à jour de données (requêtes UPDATE). Les arguments étant principalement les même, voici juste une description des deux qui diffèrent :

  • $where : prend la clause WHERE pour la mise à jour
  • $limit : vous pouvez limiter le nombre de résultats que vous mettrez à jour

($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 that you will update.

La méthode delete($table, $where = '', $limit = 0, $use_cache = true, $add_prefix = true)

...