简体   繁体   English

Zend_Db_Table_Abstract和Zend_Db_Expr

[英]Zend_Db_Table_Abstract and Zend_Db_Expr

I have recently inherited an application, written using ZF, which has various fields encrypted in the db. 我最近继承了一个使用ZF编写的应用程序,该应用程序在db中加密了各个字段。 There are many models extending Zend_Db_Table_Abstract with code similar to this example - 有许多模型使用类似此示例的代码扩展了Zend_Db_Table_Abstract-

<?php
class Partner extends Zend_Db_Table_Abstract {

    protected $_name = 'partner', $_primary = 'id';

    public function createPartner( $mobile ){

        $id = $this->insert( array(
                        'mobile' => new Zend_Db_Expr("AES_ENCRYPT('$mobile', 'random_key')"),
                        'createdOn' => date('Y-m-d H:i:s', mktime())
                    ) );

        $res = $this->find($id);
        return $res->current();
    }

}
?>

My concern with this code is that $mobile is being passed literally into the query. 我对这段代码的担心是,将$ mobile从字面上传递给查询。 What is the cleanest way to modify the way this value is being set, so that it uses quoteInto or some other method that uses place holders to parametrise the query? 修改此值的设置方式的最干净的方法是什么,以便它使用quoteInto或其他一些使用占位符对查询进行参数化的方法?

How about 怎么样

public function createPartner( $mobile ){

    $id = $this->insert( array(
                    'mobile' => new Zend_Db_Expr($this->getAdapter()->quoteInto("AES_ENCRYPT(?, 'random_key')", $mobile)),
                    'createdOn' => date('Y-m-d H:i:s', mktime())
                ) );

    $res = $this->find($id);
    return $res->current();
}

This seems to work but is there some problem with it that I am missing? 这似乎可行,但是我缺少某些问题吗?

use prepared statement in this case : 在这种情况下使用准备好的语句:

$mobile = new Zend_Db_Expr("AES_ENCRYPT('$mobile', 'random_key')");
$date = date('Y-m-d H:i:s', mktime());

$stmt = $this->getAdapter()->prepare('INSERT INTO'.$this->_name.'(mobile, createdOn) VALUES (?, ?)');
$stmt->execute(array($mobile, $date));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM