Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 (doctrine2) native sql insert

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>')))
like image 712
Ciro Vargas Avatar asked Feb 20 '23 02:02

Ciro Vargas


2 Answers

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

like image 169
Mun Mun Das Avatar answered Mar 07 '23 07:03

Mun Mun Das


In 2020 you can do something like (example query, adapt it to your params):

        $query = "
            INSERT INTO `user_challenges_claimed`
            SET
                `season_id` = :seasonId,
                `user_id` = :userId,
                `interval_type` = :intervalType,
                `is_claimed` = true
            ON DUPLICATE KEY UPDATE 
                `is_claimed` = true
            ;
            ";

        // set query params
        $queryParams = [
          'seasonId'     => $seasonId,
          'userId'       => $userId,
          'intervalType' => $intervalType,
        ];

        // execure query and get result
        $result = $this->manager->getConnection()
          ->executeQuery(
            $query,
            $queryParams
          );

        // clear manager entities
        $this->manager->clear();

        // optional - assert row has been inserted/modified
        if ($result->rowCount() < 1) {
            throw new ChallengeAlreadyClaimedException(
              "[{$seasonId}:{$userId} - {$intervalType}]"
            );
        }

$this->manager is an object implementing EntityManagerInterface (ie EntityManager).

like image 25
Konrad Gałęzowski Avatar answered Mar 07 '23 07:03

Konrad Gałęzowski