Doctrine ORM en modulos PrestaShop 8/9
Actualizado: 2024-12-01
Doctrine ORM esta disponible en modulos de PrestaShop 8+ como alternativa moderna a ObjectModel. Es ideal cuando necesitas relaciones complejas, queries DQL, o quieres aprovechar el ecosistema Symfony completo.
#Cuando usar Doctrine vs ObjectModel
| Criterio | ObjectModel | Doctrine ORM |
|---|---|---|
| Compatibilidad PS | 1.6 - 9 | 1.7.6+ (PS 8 recomendado) |
| Multilingue nativo | Si (_lang table) | Manual con relaciones |
| Curva de aprendizaje | Baja | Media-alta |
| Relaciones complejas | Manual | ManyToOne, OneToMany, ManyToMany |
| Testing con mocks | Dificil | Facil con EntityManager mock |
| Recomendado para | Modulos simples / compatibilidad | Modulos complejos PS 8+ |
#Definir una entidad Doctrine
src/Entity/MyItem.php — entidad con anotaciones
php
<?php
declare(strict_types=1);
namespace MyModule\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(name="mymodule_item")
* @ORM\Entity(repositoryClass="MyModule\Repository\MyItemRepository")
*/
class MyItem
{
/**
* @ORM\Id
* @ORM\Column(name="id_mymodule_item", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private int $id;
/**
* @ORM\Column(name="name", type="string", length=255)
*/
private string $name = '';
/**
* @ORM\Column(name="active", type="boolean")
*/
private bool $active = true;
/**
* @ORM\Column(name="sort_order", type="integer", options={"default":0})
*/
private int $sortOrder = 0;
public function getId(): int { return $this->id; }
public function getName(): string { return $this->name; }
public function setName(string $name): self { $this->name = $name; return $this; }
public function isActive(): bool { return $this->active; }
public function setActive(bool $active): self { $this->active = $active; return $this; }
}
#Instalar/desinstalar la tabla con SchemaTool
Instalar y desinstalar tablas Doctrine desde el modulo
php
<?php
use Doctrine\ORM\Tools\SchemaTool;
use MyModule\Entity\MyItem;
public function install(): bool
{
return parent::install()
&& $this->installDoctrineTables();
}
public function uninstall(): bool
{
return $this->uninstallDoctrineTables()
&& parent::uninstall();
}
private function installDoctrineTables(): bool
{
try {
$em = $this->get('doctrine.orm.default_entity_manager');
$schemaTool = new SchemaTool($em);
$metadata = $em->getMetadataFactory()->getMetadataFor(MyItem::class);
$schemaTool->createSchema([$metadata]);
return true;
} catch (\Exception $e) {
return false;
}
}
private function uninstallDoctrineTables(): bool
{
try {
$em = $this->get('doctrine.orm.default_entity_manager');
$schemaTool = new SchemaTool($em);
$metadata = $em->getMetadataFactory()->getMetadataFor(MyItem::class);
$schemaTool->dropSchema([$metadata]);
return true;
} catch (\Exception $e) {
return false;
}
}
#Usar el Entity Manager
CRUD con Entity Manager de Doctrine
php
<?php
$em = $this->get('doctrine.orm.default_entity_manager');
// ── CREATE ──
$item = new MyItem();
$item->setName('Nuevo item');
$item->setActive(true);
$em->persist($item);
$em->flush();
// ── READ ──
$item = $em->find(MyItem::class, 42);
$repo = $em->getRepository(MyItem::class);
$all = $repo->findAll();
$active = $repo->findBy(['active' => true], ['sortOrder' => 'ASC']);
// ── UPDATE ──
$item = $em->find(MyItem::class, 42);
$item->setName('Nombre actualizado');
$em->flush();
// ── DELETE ──
$item = $em->find(MyItem::class, 42);
$em->remove($item);
$em->flush();
Descargar en Markdown
Pensado para pegar en ChatGPT, Claude u otra IA. Incluye solo el contenido de esta pagina.