教义原始 sql 和准备好的语句

Doctrine raw sql and prepared statements(教义原始 sql 和准备好的语句)
本文介绍了教义原始 sql 和准备好的语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用准备好的语句的 Doctrine_RawSql 查询.但是,在生成 SQL 查询时,它们似乎被忽略了.但是如果我省略了标记值,我会得到一个关于绑定变量数量不匹配的异常(所以它至少试图将它们分入).

I've got a Doctrine_RawSql query using prepared statements. However, they seem to get ignored when the SQL query is generated. But If I leave out the token values, I get an exception about number of bound variables not matching (so it's at least trying to sub them in).

如果我在内联包含这些值,Doctrine 是否在幕后做任何事情来防止 SQL 注入?

If I include these values inline, is Doctrine doing anything behind the scenes to prevent SQL injection?

这是我的代码:

public function sortedPhotogsByLocation($location)
{
    $q = new Doctrine_RawSql();
    $result = $q->select('{p.*}')
            ->from('photographers p')
            ->addComponent('p', 'Photographer')
            ->where('p.city_id = ?', $location->id)
            ->orderBy('CASE WHEN p.lname < "?%" THEN 1 ELSE 0 END, p.lname ASC', $location->photographer_sort)
            ->execute();
    return $result;
}

这提供了以下 SQL 输出:

This provides the following SQL output:

  SELECT *  
  FROM photographers p 
  WHERE p.city_id = ? 
  ORDER BY 
    CASE WHEN p.lname < "?%" THEN 1 ELSE 0 END, p.lname 
  ASC

$location 上的属性设置正确.如果我对参数进行硬编码:

The properties on $location are being set properly. If I hardcode the parameters:

->where('p.city_id = ?', 5)

我遇到了同样的问题,令牌没有被替换.

I encounter the same problem with the tokens not being replaced.

推荐答案

我对Doctrine_RawSql并不完全熟悉,但是占位符应该是单独的,而不是?%",只是?并在您传递的变量上添加 % .看看 示例 #6.

I'm not entirely familiar with Doctrine_RawSql, but a placeholder should be by itself, not "?%", just ? and add the % on the variable you are passing. Take a look at example #6.

这篇关于教义原始 sql 和准备好的语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)