在Doctrine中使用URL字段

时间:2010-06-23 20:26:13

标签: php mysql openid doctrine

我正在构建网站并计划实施OpenID。我可以从Google获得一个OpenID网址,但是Doctrine似乎在我的查询的where子句中对URL做了一些有趣的事情。我该如何解决这个问题?

这是函数

/* This function queries docrtrine for a user OpenID URL
 * and returns the user object.
 */
function getUserByUserOpenIDURL ($userOpenIDURL) {
  $q = Doctrine_Query::create()
     ->select('*')
     ->from('jsgUserOpenID as u')
     ->where('openid_url = ' . $userOpenIDURL);

  return $q->fetchOne();
}

以下是页面中的错误

  

致命错误:未捕获的异常'Doctrine_Exception',并在/Library/WebServer/Documents/ResearchPM/lib/Doctrine/Table.php:299中显示消息'找不到类www'堆栈跟踪:#0 / Library / WebServer / Documents / ResearchPM / lib / Doctrine / Table.php(256):Doctrine_Table-> initDefinition()#1 /Library/WebServer/Documents/ResearchPM/lib/Doctrine/Connection.php(1126):Doctrine_Table-> __ construct( 'www',Object(Doctrine_Connection_Mysql),true)#2 /Library/WebServer/Documents/ResearchPM/lib/Doctrine/Query.php(1934):Doctrine_Connection-> getTable('www')#3 / Library / WebServer / Documents / ResearchPM / lib / Doctrine / Query.php(1732):Doctrine_Query-> loadRoot('www','www')#4 /Library/WebServer/Documents/ResearchPM/lib/Doctrine/Query.php(713) :Doctrine_Query-> load('www.google')#5 /Library/WebServer/Documents/ResearchPM/lib/Doctrine/Query/Where.php(121):Doctrine_Query-> parseClause('https://www.goo。 ..')#6 /Library/WebServer/Documents/ResearchPM/lib/Doctrine/Query/Where.php(81):Doctrine_Query_Where- > _buildSql('openid_url','=','https://www.goo ..在/Library/WebServer/Documents/ResearchPM/lib/Doctrine/Table.php第299行

1 个答案:

答案 0 :(得分:4)

你没有适当地逃避变量。 命名或位置通配符有两种方法:

$q = Doctrine_Query::create()
       ->select('*')
       ->from('jsgUserOpenID as u')
       ->where('openid_url = ?', $userOpenIDURL);

或者

$q = Doctrine_Query::create() 
       ->select('*') 
       ->from('jsgUserOpenID as u') 
       ->where('openid_url = :url', array("url" => $userOpenIDURL));

这样可以正确地转义您正在插入的变量,并使您的应用程序免受sql-injection

的攻击