4

如何在本机 sql 上的 symfony2 学说 2 中插入数据?

我的查询

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

3 回答 3

7

你必须使用$conn->insert('table', $dataArray);. 查看文档

于 2012-09-22T03:02:34.590 回答
1

在 2020 年,您可以执行以下操作(示例查询,使其适应您的参数):

        $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是一个对象实现EntityManagerInterface(即EntityManager)。

于 2020-11-15T16:44:23.817 回答
0

通常你不会在 symfony 2 项目中使用所谓的原生 sql,而是使用高级 Doctrine ORM 层。

然而,有一个学说 dbal 层可以启用例如 mysql 查询而不是 DQL。这是参考

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

于 2012-09-21T22:39:48.167 回答