Symfony2 (doctrine2) native sql insert

2019-07-08 20:13发布

问题:

How to insert data in symfony2 doctrine2 on native sql?

My query

insert into propriedades (id,name,descripcion,num_lote,cod_imovel,imovel,convenio,proprietar,cpf,area_ha,perimetro,location,centro) VALUES (nextval('propriedades_id_seq'),'?','?','?','?','?','?','?','?','?','?',ST_GeomFromKML('<Polygon><outerBoundaryIs><LinearRing><coordinates>".$terra['coordinates']."</coordinates></LinearRing></outerBoundaryIs></Polygon>'),ST_Centroid(ST_GeomFromKML('<Polygon><outerBoundaryIs><LinearRing><coordinates>".$terra['coordinates']."</coordinates></LinearRing></outerBoundaryIs></Polygon>')))

回答1:

You have to use $conn->insert('table', $dataArray);. See documentation



回答2:

Usually you do not use what you call native sql in a symfony 2 project but the high level Doctrine ORM layer.

However there is a doctrine dbal layer which enables e.g. mysql queries instead of DQL. Here is the reference

http://symfony.com/doc/2.0/cookbook/doctrine/dbal.html