5

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>')))

3 Answers 3

8

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

Sign up to request clarification or add additional context in comments.

4 Comments

this dont work with me, because i use psql in queryes and in insert this had in '', transforming the function in a string
hmm, you can put function strings in a variable then pass it to the $dataArray.
discard previous comment. I misunderstood your problem.
You can try with $conn->executeQuery(). From the doc it looks flexible enough.
2

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).

Comments

0

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

1 Comment

i used the function query() but this dont have statements support, have other function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.