Warning: You are browsing the documentation for PrestaShop 1.7, which is outdated.
You might want to read an updated version of this page for the current version, PrestaShop 8. Read the updated version of this page
The database structure of PrestaShop can be found in install/data/db_structure.sql
(1.7.8.0 releases example).
It is used one time, during the installation of PrestaShop. It contains the structure of almost all tables. If a table needs to be added or modified, this the file you should open.
With PrestaShop 1.7, some tables are now linked to Doctrine entities
(i.e stocks). If their ObjectModel
(= legacy) equivalent does not exist,
the entity is probably only managed by Doctrine.
In that case, updating the table can be done by modifying the related entity
stored in src/PrestaShopBundle/Entity/
.
The default database content is stored in XML files in install/data/xml/
.
There is one file per entity (= table). XML files are structured following ObjectModel structure.
These files are used during the PrestaShop installation as well.
Another file is being used to load data during the install process: install/data/db_data.sql
. Some versions of PrestaShop do not use it so you might not always find it in the ZIP archive.
Once PrestaShop is installed, the default structure and content files we saw are not used anymore.
If a new release of PrestaShop must bring changes to the existing database, an
upgrade file must be created along the db_structure.sql
update. This SQL
file will be stored in the folder /install/upgrade/sql/
.
Its name is the PS version on which the change will be applied.
For instance, here is the file 1.7.8.0.sql, used by shops upgrading to 1.7.8.0 or later:
[...]
UPDATE `PREFIX_product` SET `product_type` = "virtual" WHERE `is_virtual` = 1;
/* PHP:ps_1780_add_feature_flag_tab(); */;
In there we can read the SQL queries to execute when upgrading to 1.7.8.0.
Each of them alters the structure and/or modify the existing data.
In case you have complex algorithms to run, you can call PHP code with the
PHP:
keyword.
To make the code callable, a dedicated file has to be created in
/install/upgrade/php/
with a function in it. This file and function must have
the same name as we saw in the SQL upgrade file.
If we reuse the previous example, we will find the corresponding file /install/upgrade/php/ps_1780_add_feature_flag_tab.php:
<?php
function ps_1780_add_feature_flag_tab()
{
// Code inserting values in database
[...]
}
PrestaShop does not expect these functions to return anything. It will always consider it was run without failure.
Applying the changes on your database can be done:
install/upgrade/upgrade.php
PrestaShop lists the upgrade files waiting to be applied, by selecting the names
fitting between the configuration property PS_VERSION_DB
and the constant
_PS_INSTALL_VERSION_
defined in install/install_version.php
.
In the top of this page, we talked about entities being managed only by Doctrine. Applying the changes on the database is done with the following command:
php bin/console prestashop:schema:update-without-foreign
php app/console
instead of php bin/console
for versions prior to
1.7.4