编程语言
首页 > 编程语言> > php – 我的Zend框架’引用’混乱

php – 我的Zend框架’引用’混乱

作者:互联网

我有一个非常简单的问题,我无法在Zend Framework手册或其他地方找到令人满意的(主观看到的)答案……

我有很多方法可以将我的php变量移交给我的sql查询,但我失去了概述,可能我对一般的引用缺乏了解.

准备好的陈述

$sql =  "SELECT this, that
        FROM table
        WHERE id = ? AND restriction = ?";

$stmt = $this->_db->query($sql, array($myId, $myValue)); 
$result = $stmt->fetchAll();

我理解使用这个解决方案我不需要引用任何东西,因为db为我处理这个.

通过API查询Zend_Db_Table和_Row对象

$users = new Users();

a) $users->fetchRow('userID = ' . $userID);  
b) $users->fetchRow('userID = ' . $users->getAdapter()->quote($userID, 'INTEGER'));  
c) $users->fetchRow('userID = ?', $userID);  
d) $users->fetchRow('userID = ?', $users->getAdapter()->quote($userID, 'INTEGER'));  

问题

我明白a)不好,因为根本没有引用.但是其他版本呢,最好的是什么?是c)被视为一个陈述并自动引用或我需要使用d)当我使用?标识?

解决方法:

免责声明:此信息自本答复的原始发布日期起有效. ZF经常更改,此信息可能会在将来的版本中过时,但是,对于存档目的,这将保持不变.

如果将字符串传递给Zend_Db_Table_Abstract(您正在执行)的子类的fetchRow()方法,则它将被视为Zend_Db_Table_Select实例的where部分.

换句话说,在内部,Zend_Db_Table执行此操作:

if (!($where instanceof Zend_Db_Table_Select)) {
    $select = $this->select();

    if ($where !== null) {
        $this->_where($select, $where);
    }

所以…:

a) $users->fetchRow('userID = ' . $userID);  

根本没有引用.

b) $users->fetchRow('userID = ' . $users->getAdapter()->quote($userID, 'INTEGER'));  

手动引用为整数.

c) $users->fetchRow('userID = ?', $userID);  

由Zend_Db_Adapter _ * :: quoteInto()自动引用

d) $users->fetchRow('userID = ?', $users->getAdapter()->quote($userID, 'INTEGER'));

实际上是双引号,一次由你引用,一次通过自动引用.

就“最佳”而言,我建议选项C.框架将自动调用参数化值的quoteInto.

请记住:您总是可以将Zend_Db_Table_Select或Zend_Db_Select的实例传递给fetchRow()方法,而不是…

再次,在Zend_Db_Table_Abstract的子类中,它看起来像这样:

$this->fetchRow($this->select()->where('userID = ?', $userID));

这样做的好处是,您可以构建更复杂的查询,因为您可以控制很多,而不仅仅是SQL查询的WHERE子句.从理论上讲,您可以轻松地做到:

$select = $this->select()->where('userID = ?', $userID)
                         ->join(array('sat' => 'superAwesomeTable'), array('sat.user_id = userID', array('superAwesomeColumn'));

$this->fetchRow($select);

注意:如果传递了Zend_Db_Select的实例,则fetchRow()方法的作用与fetchAll()完全相同,只是它在内部调用select对象的limit()方法,参数为1.

标签:php,sql,zend-framework,quoting
来源: https://codeday.me/bug/20190622/1260134.html