Symfony2(doctrine2)本机sql插入

时间:2012-09-21 22:08:28

标签: symfony doctrine doctrine-orm persistence

如何在本机sql上的symfony2 doctrine2中插入数据?

我的查询

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 个答案:

答案 0 :(得分:5)

您必须使用$conn->insert('table', $dataArray);。见documentation

答案 1 :(得分:0)

通常,您不会在symfony 2项目中使用您称为本机sql的内容,而是使用高级Doctrine ORM层。

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

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

答案 2 :(得分:0)

在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)的对象。