我对数据库的 Doctrine 查询有疑问,我不确定哪里出了问题。 这是交易: 这是我用来创建数据库的实际 mysql 查询:
create table productos (
codigoProducto int (8) unsigned auto_increment primary key,
nombre varchar(100) not null,
id_categoria smallint(3) unsigned,
descripcion text,
foto varchar(100),
precio smallint (4) unsigned not null,
visitado int (7),
habilitado boolean,
foreign key (id_categoria)
references categorias(id)
on update cascade
on delete restrict
)engine=innoDB;
create table facturas(
id int (8) unsigned auto_increment primary key,
id_comprador int (7) unsigned,
metodo_pago boolean,
forma_entrega boolean,
fecha datetime,
foreign key (id_comprador)
references compradores(id)
on update cascade
on delete cascade
)engine=innoDB;
create table detallefactura(
id int(8) unsigned auto_increment primary key,
id_producto int(8) unsigned,
id_factura int(8) unsigned,
cantidad smallint (4),
foreign key(id_producto)
references productos(codigoProducto)
on update cascade
on delete cascade,
foreign key(id_factura)
references facturas(id)
on update cascade
on delete cascade
)engine=innoDB;
“detalleFactura”是“productos”和“facturas”的连接表,以便每张发票包含 1 个以上的产品。因此,对于以下内容,我试图将购买的所有产品都放在一张发票中:
static function traerProductosComprados($id){
$query = Doctrine_Query::create()
->select('d.id, p.nombre, p.precio, d.cantidad')
->from('Detallefactura d')
->leftjoin('d.Productos p')
->where('d.id_factura = ?',$id);
return $query->execute()->toArray();
}
如果我回显查询,它会显示以下内容:
SELECT d.id, p.nombre, p.precio, d.cantidad FROM Detallefactura d LEFT JOIN d.Productos p WHERE d.id_factura = ?
当我将其粘贴到 phpmyadmin 时,它会抛出以下错误:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE d.id_factura = ? LIMIT 0, 30' at line 1;
我现在没主意了,如果还有什么我想念的,请告诉我!
最佳答案
您需要为左连接指定 ON
子句
关于php - 左连接的 MySQL/doctrine 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13967573/