php - Zend_Log_Writer_Db - 调用未定义的方法 Zend_Config::insert()

标签 php database zend-framework logging undefined

我正在运行 php 5.2.12 和 Zend Framework 5.0.2

在我的 Bootstrap.php 中,我初始化我们的数据库连接,并初始化我们的记录器...将两者都放在注册表中。

但是,当我尝试在 IndexController.php 中记录信息时,它给出以下消息: “* fatal error :在第 137 行调用/usr/local/zendsvr/share/ZendFramework/library/Zend/Log/Writer/Db.php 中未定义的方法 Zend_Config::insert()*”

在本文的底部,您将找到 Zend Framework 的类文件 db.php 以及正在调用的 _write 函数。

我认为问题是我从 application.ini 中获取数据库连接选项...并且在 application.ini 中没有为数据库定义 insert() 函数。但我真的不知道如何向配置中添加一个,或者我应该如何执行此操作。

Bootstrap.php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    protected function _initConfig()
    {
        Zend_Registry::set('config', new Zend_Config($this->getOptions()));
    }

    protected function _initDatabases()
    {    
        $this->bootstrap('multidb');
        $resource = $this->getPluginResource('multidb');

        $databases = Zend_Registry::get('config')->resources->multidb;

        foreach ($databases as $name => $adapter)
        {
            $db_adapter = $resource->getDb($name);

            Zend_Registry::set($name, $db_adapter);
        }

    }

    protected function _initLog()
    {
        $db             = Zend_Registry::get('config')->resources->multidb->as400;

        $columnMapping  = array('ILGID'     => 'id',        //1 numeric
                                'ILGLVL'    => 'priority',  //2 numeric
                                'ILGDTE'    => 'date',      //yymmdd
                                'ILGTME'    => 'time',      //hhmmss
                                'ILGPGM'    => 'program',   //40 alnum
                                'ILGURL'    => 'url',       //2100
                                'ILGUSR'    => 'user',      //30
                                'ILGMSG'    => 'message');  //1000

        $writer         = new Zend_Log_Writer_Db($db, 'dwhlib.intralog', $columnMapping);
        $logger         = new Zend_Log($writer);


        $date = new Zend_Date();
        date_default_timezone_set('America/Chicago');


        $logger->setEventItem('id'      , 1);
        $logger->setEventItem('date'    , $date->get('Ymd'));
        $logger->setEventItem('time'    , $date->get('Hms'));
        $logger->setEventItem('program' , 'testProgramName');                   $logger->setEventItem('url'     , $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
        $logger->setEventItem('user'    , gethostbyaddr($_SERVER['REMOTE_ADDR']));


        Zend_Registry::set('logger', $logger);
    }

}

application.ini

resources.multidb.as400.adapter = "db2"
resources.multidb.as400.host = "i5"
resources.multidb.as400.username = "removedUsername"
resources.multidb.as400.password = "removedPassword"
resources.multidb.as400.dbname = "*LOCAL"
resources.multidb.as400.default = true

IndexController.php

include("/www/zendserver/htdocs/development/application/models/as400.php");
class IndexController extends Zend_Controller_Action
{
    public function init()
    {       
        Zend_Registry::get('logger')->info("this is a test message");
    }

    public function indexAction()
    {
        // action body
    }
}

as400.php

Class default_Model_As400 extends Zend_Db {


    public static function ExecuteSelect($sql, $mode = Zend_Db::FETCH_ASSOC, $log = false)
    {
        $stmt = self::getStmt($sql);
        $stmt->setFetchMode($mode);
        $stmt->execute();

        if($log === true) {
            Zend_Registry::get('logger')->info($sql);
        }

        $rows = $stmt->fetchAll();

        return $rows;
    }

    private static function getStmt($sql){
        $db = Zend_Registry::get('config')->resources->multidb->as400;

        $abstractAdapter = new Zend_Db_Adapter_Db2($db);

        return new Zend_Db_Statement_DB2($abstractAdapter, $sql);
    }

    public function insert($libAndFile, $arrData){
        echo "this was hit!!";
    }
}

db.php

class Zend_Log_Writer_Db extends Zend_Log_Writer_Abstract
{
    public function __construct($db, $table, $columnMap = null)
        {
            $this->_db    = $db;
            $this->_table = $table;
            $this->_columnMap = $columnMap;
        }


    protected function _write($event)
    {
        if ($this->_db === null) {
            require_once 'Zend/Log/Exception.php';
            throw new Zend_Log_Exception('Database adapter is null');
        }

        if ($this->_columnMap === null) {
            $dataToInsert = $event;
        } else {
            $dataToInsert = array();
            foreach ($this->_columnMap as $columnName => $fieldKey) {
                $dataToInsert[$columnName] = $event[$fieldKey];
            }
        }

        $this->_db->insert($this->_table, $dataToInsert);
    }
}

最佳答案

  • 发生了什么:您正在 Zend_Config 实例上调用一个名为 insert() 的方法。
  • 您想要的:通过 Zend_Db_Adapter 调用方法 insert()

您的 _initLog() 引导方法有问题:

$db = Zend_Registry::get('config')->resources->multidb->as400;
$writer = new Zend_Log_Writer_Db($db, 'dwhlib.intralog', $columnMapping);

Zend_Log_Writer_Db 期望 Zend_Db 适配器作为第一个构造函数参数。要解决此问题,由于您已经在注册表中注册了数据库适配器,因此您应该执行以下操作:

$dbName = Zend_Registry::get('config')->resources->multidb->as400;
$db = Zend_Registry::get($dbName);
$writer = new Zend_Log_Writer_Db($db, 'dwhlib.intralog', $columnMapping);

关于php - Zend_Log_Writer_Db - 调用未定义的方法 Zend_Config::insert(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9486300/

相关文章:

php - Symfony:如何从文件加载自定义捆绑配置?

php - 如何为团队设置自定义顺序

php - 从租赁表中租用可用的汽车

java - 根据列中的重复项从数据中删除整行

sql - 如何删除 SQL 中的重复行(Clickhouse)?

mysql - ZF - 使用 CASE WHEN 的 MySQL SUM 查询

PHP mysql charset utf8问题

php - mysql/php - 如何实现独特的 View 系统?

php - PHP 应用程序中的 MySQL 字符编码 (öä)

php - reCAPTCHA (Zend_Service_ReCaptcha) 抛出 "Unable to Connect to tcp://api-verify.recaptcha.net:80"