View Issue Details

IDProjectCategoryView StatusLast Update
72PHPOF2Defaultpublic18 Sep 2008 16:44
Reportermrosenquist Assigned Totimj  
PrioritynormalSeverityminorReproducibilityhave not tried
Status closedResolutionfixed 
Target Version0.10.1Fixed in Version0.10.1 
Summary72: Use of Audit drivers could cause the creation of Two mysqli connections
DescriptionCurrently a instantiated AuditDriver Object needs to be passed in to DB::factory()
in the options.

#Example realworld objlib config:
#// --- The main database connection
#require_once 'PHPOF2/DB.php';
#require_once 'PHPOF2/Audit/GenericDb.php';
#
#// Let's store the audit data in the same database as the main app data
#$dsn1 = Objlib::get(Objlib::TYPE_CONFIG)->global->dsn1;
#
#$db_options = array(
# 'audit_driver' => new PHPOF2_Audit_GenericDb($dsn1)
#);
#
#$options = array(
# //Moved to options
# Objlib::OPTION_METHOD_PARAMS => array($dsn1, $db_options),
# Objlib::OPTION_IS_DEFAULT => true
#);
#Objlib::setAutoLoad(Objlib::TYPE_DB, 'db1', array('PHPOF2_DB','factory'), #$options);

It would be helpful to pass-in just a String of the class to use
new $audit_class($db, $options['audit_options']);
TagsNo tags attached.
Attached Files
Patch_GenericDb.php.patch (482 bytes)   
--- GenericDb.php.old	2008-07-21 11:05:33.000000000 +0100
+++ GenericDb.php	2008-07-21 11:05:44.000000000 +0100
@@ -41,7 +41,7 @@
 	public function __construct(&$dsn, $options = array())
 	{
 		if (is_object($dsn)) {
-			if ($dsn instanceof MDB2) {
+			if ($dsn instanceof MDB2 || $dsn instanceof MDB2_Driver_Common) {
 				$this->_db = &$dsn;
 			} else {
 				throw new Exception('PHPOF2_Audit_GenericDb was passed an object which was not an MDB2 object in the "dsn" parameter');
Patch_GenericDb.php.patch (482 bytes)   
DB.php.patch (1,470 bytes)   
--- DB.php.old	2008-07-21 10:35:14.000000000 +0100
+++ DB.php	2008-07-21 11:03:38.000000000 +0100
@@ -41,7 +41,7 @@
 	{
 		// Options that are specific to PHPOF2_DB and are not to be passed to MDB2
 		// (MDB2 throws errors if it gets options it doesn't know about)
-		$private_option_list = array('audit_driver');
+		$private_option_list = array('audit_driver', 'audit_driver_options');
 		$private_options = array();
 		if (is_array($options)) {
 			foreach ($options as $key => $val) {
@@ -70,11 +70,22 @@
 		}
 		
 		// Enable audit driver if required
-		if (isset($private_options['audit_driver'])) {
-			if (!$private_options['audit_driver'] instanceof PHPOF2_Audit_Interface) {
+		if (isset($private_options['audit_driver'])) {		
+			// If the driver is a string try and load the class
+			if (is_string($private_options['audit_driver'])) {
+				$driver = $private_options['audit_driver'];
+				if (isset($private_options['audit_driver_options'])) {
+					$mdb2->phpof2_auditor = new $driver($mdb2, $private_options['audit_driver_options']);
+				} else {
+					$mdb2->phpof2_auditor = new $driver($mdb2);
+				}
+			} else {
+				$mdb2->phpof2_auditor = $private_options['audit_driver'];
+			}
+			
+			if (!$mdb2->phpof2_auditor instanceof PHPOF2_Audit_Interface) {
 				throw new Exception("Audit driver passed to PHPOF2_DB is not a PHPOF2_Audit_Interface object");
 			}
-			$mdb2->phpof2_auditor = $private_options['audit_driver'];
 		}
 		return $mdb2;
 	}
DB.php.patch (1,470 bytes)   
Audited.php.patch (311 bytes)   
--- DBRow/Audited.php.old	2008-07-21 15:16:12.000000000 +0100
+++ DBRow/Audited.php	2008-07-21 15:17:15.000000000 +0100
@@ -83,6 +83,8 @@
 	{
 		if ($this->skip_audit) return;
 		
+		if (!isset($this->db->phpof2_auditor)) return;
+		
 		$changes = $this->getChanges();
 		
 		if ($this->audit_owner === null) {
Audited.php.patch (311 bytes)   
Audit_README.txt.patch (713 bytes)   
--- README.txt.old	2008-07-21 15:22:04.000000000 +0100
+++ README.txt	2008-07-21 17:14:55.000000000 +0100
@@ -51,8 +51,22 @@
 $db_options = array('audit_driver' => $audit_driver);
 $db = PHPOF2_DB::factory($dsn, $db_options);
 
+OR
+
+<?php
+require_once 'PHPOF2/DB.php';
+require_once 'PHPOF2/Audit/GenericDb.php';
+
+// The DSN to the main application database
+$dsn = 'mysqli://foo:password@foohost/somedb';
+
+$db = PHPOF2_DB::factory($dsn, array('audit_driver' => 'PHPOF2_Audit_GenericDb'));
+
 class widgets extends PHPOF2_DBTable {
-	protected $audit_logging = true;
+	protected $default_row_class = 'widget';
+}
+
+class widget extends PHPOF2_DBRow_Audited {
 }
 
 $widgets = new widgets($db, 'widgets');
Audit_README.txt.patch (713 bytes)   

Activities

mrosenquist

21 Jul 2008 11:08

reporter   ~59

Added a small fix to audit driver of allow insurances of MDB2_Driver_Common for $dsn

mrosenquist

21 Jul 2008 11:15

reporter   ~60

Included a patch to added the class loading functionality.

#Example of Oblib conf
$options = array(
    //Moved to options
    Objlib::OPTION_METHOD_PARAMS => array(
        Objlib::get(Objlib::TYPE_CONFIG)->global->dsn1,
        array('audit_driver' => 'PHPOF2_Audit_GenericDb')
    ),
    Objlib::OPTION_REQUIRED_FILES => array(
        'PHPOF2/DB.php',
        'PHPOF2/Audit/GenericDb.php'
    ),
    Objlib::OPTION_IS_DEFAULT => true
);
Objlib::setAutoLoad(Objlib::TYPE_DB, 'db1', array('PHPOF2_DB','factory'), $options);

mrosenquist

21 Jul 2008 17:31

reporter   ~61

protected $audit_columns_ignore = array(); in DBTable is not uses, It could be moved to the row class now

timj

23 Jul 2008 21:53

manager   ~62

SVN r1141 has the fix to make it possible to use PHPOF2_DBRow_Audited objects even if no audit driver is loaded

timj

23 Jul 2008 21:56

manager   ~63

SVN r1142 has the fix to allow audit drivers to be passed by name instead of instance to PHPOF2_DB::factory()

timj

23 Jul 2008 21:57

manager   ~64

SVN r1143 updates the docs as per the README patch

timj

23 Jul 2008 22:04

manager   ~65

SVN r1144 fixes the problem with passing an MDB2 object direct to PHPOF2_Audit_GenericDb, although I have modified your patch because there was a bug in PHPOF - there is no need to allow MDB2-derived objects through, only those derived from MDB2_Driver_Common.

Issue History

Date Modified Username Field Change
21 Jul 2008 10:32 mrosenquist New Issue
21 Jul 2008 11:07 mrosenquist File Added: Patch_GenericDb.php.patch
21 Jul 2008 11:08 mrosenquist Note Added: 59
21 Jul 2008 11:13 mrosenquist File Added: DB.php.patch
21 Jul 2008 11:15 mrosenquist Note Added: 60
21 Jul 2008 15:19 mrosenquist File Added: Audited.php.patch
21 Jul 2008 17:31 mrosenquist Note Added: 61
21 Jul 2008 17:32 mrosenquist File Added: Audit_README.txt.patch
23 Jul 2008 21:53 timj Note Added: 62
23 Jul 2008 21:56 timj Note Added: 63
23 Jul 2008 21:57 timj Note Added: 64
23 Jul 2008 21:57 timj Status new => assigned
23 Jul 2008 21:57 timj Assigned To => timj
23 Jul 2008 22:04 timj Note Added: 65
23 Jul 2008 22:04 timj Status assigned => resolved
23 Jul 2008 22:04 timj Fixed in Version => 0.10.1
23 Jul 2008 22:04 timj Resolution open => fixed
24 Jul 2008 11:06 timj Target Version => 0.10.1
18 Sep 2008 16:44 timj Status resolved => closed