🗄️ Base de Datos
Documentacion completa del sistema de base de datos de PrestaShop: ObjectModel, DbQuery Builder, schema de tablas y buenas practicas de seguridad.
🔷
ObjectModel
$definition, CRUD, multilang, validacion, relaciones y WebService
🔍
DbQuery Builder
API fluida para construir queries SQL de forma segura y tipada
📊
Schema Browser
Explorador interactivo de las 80+ tablas del core de PrestaShop
🛡️
Seguridad
pSQL(), (int) casting, bqSQL(), prevencion de SQL injection
➕
Tablas personalizadas
Crear tablas en modulos: naming, install SQL, ObjectModel, upgrades
⚡
Rendimiento
Cache de queries, indices, EXPLAIN, evitar N+1, optimizacion
ObjectModel — Referencia rapida
src/Entity/MyEntity.php
php
<?php
class MyEntity extends ObjectModel
{
public $name;
public $description;
public $active;
public $price;
public $date_add;
public $date_upd;
public static $definition = [
'table' => 'mymodule_entity',
'primary' => 'id_mymodule_entity',
'multilang' => true, // Habilita campos multiidioma
'multilang_shop' => false, // true = multitienda + multiidioma
'fields' => [
// Campos simples
'active' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'],
'price' => ['type' => self::TYPE_FLOAT, 'validate' => 'isPrice'],
'date_add' => ['type' => self::TYPE_DATE, 'validate' => 'isDate'],
'date_upd' => ['type' => self::TYPE_DATE, 'validate' => 'isDate'],
// Campos multiidioma (requiere tabla _lang)
'name' => [
'type' => self::TYPE_STRING,
'lang' => true,
'validate' => 'isCatalogName',
'required' => true,
'size' => 255,
],
'description' => [
'type' => self::TYPE_HTML,
'lang' => true,
'validate' => 'isCleanHtml',
'allow_html' => true,
],
],
];
// CRUD
$entity = new MyEntity();
$entity->active = 1;
$entity->name = ['es' => 'Nombre', 'en' => 'Name']; // multilang
$entity->add(); // INSERT
$entity = new MyEntity(1); // Load by ID
$entity->active = 0;
$entity->update(); // UPDATE
$entity->delete(); // DELETE
}
DbQuery Builder — Referencia rapida
php
<?php
// Consulta simple
$result = Db::getInstance()->executeS(
(new DbQuery())
->select('p.id_product, pl.name, p.price')
->from('product', 'p')
->leftJoin('product_lang', 'pl', 'p.id_product = pl.id_product AND pl.id_lang = ' . (int)$id_lang)
->where('p.active = 1')
->where('p.price > ' . (float)$min_price)
->orderBy('p.date_add DESC')
->limit(20, 0) // limit, offset
->build() // retorna el SQL string
);
// Insertar
Db::getInstance()->insert('mymodule_data', [
'id_order' => (int)$order->id,
'data' => pSQL(json_encode($data)),
'created_at' => date('Y-m-d H:i:s'),
]);
// Actualizar
Db::getInstance()->update('mymodule_data',
['status' => 1],
'id_order = ' . (int)$id_order
);
// Eliminar
Db::getInstance()->delete('mymodule_data', 'id_order = ' . (int)$id_order);