After some trying of my own. It turns out this wont work using the existing events.
This may work still to override the $sql statment, but it will not work with the $result. Since the $result is a mysql resource and not an actual dataset it is useless. So disregard all of the above.
I have accomplished this when some new events below. Ive only test this with the module I am building. In phproad/modules/db/classes/db_sqlbase.php:
/**
* Fetches all SQL result rows as a sequential array.
*
* @param string $sql An SQL SELECT statement.
* @param array $bind Data to bind into SELECT placeholders.
* @return array
*/
public function fetchAll($sql, $bind = null)
{
if (Backend::$events) {
$fetch_event = Backend::$events->fireEvent('core:onBeforeDatabaseFetch', $sql);
if( isset($fetch_event[0]['result']) ) {
return $fetch_event[0]['result'];
}
}
$result = $this->query($this->prepare($sql, $bind));
$fetch = $this->_fetchAll($result);
if (Backend::$events)
Backend::$events->fireEvent('core:onAfterDatabaseFetch', $sql, $fetch);
return $fetch;
}
/**
* Fetches the first column of all SQL result rows as an array.
*
* The first column in each row is used as the array key.
*
* @param string $sql An SQL SELECT statement.
* @param array $bind Data to bind into SELECT placeholders.
* @return array
*/
public function fetchCol($sql, $bind = null)
{
if (Backend::$events) {
$fetch_event = Backend::$events->fireEvent('core:onBeforeDatabaseFetch', $sql);
if( isset($fetch_event[0]['result']) ) {
return $fetch_event[0]['result'];
}
}
$result = $this->query($this->prepare($sql, $bind));
$fetch = $this->_fetchAll($result, 0);
if (Backend::$events)
Backend::$events->fireEvent('core:onAfterDatabaseFetch', $sql, $fetch);
return $fetch;
}
/**
* Fetches the first column of the first row of the SQL result.
*
* @param string $sql An SQL SELECT statement.
* @param array $bind Data to bind into SELECT placeholders.
* @return string
*/
public function fetchOne($sql, $bind = null)
{
if (Backend::$events) {
$fetch_event = Backend::$events->fireEvent('core:onBeforeDatabaseFetch', $sql);
if( isset($fetch_event[0]['result']) ) {
return $fetch_event[0]['result'];
}
}
$result = $this->query($this->prepare($sql, $bind));
$fetch = $this->driver()->fetch($result, 0);
if (Backend::$events)
Backend::$events->fireEvent('core:onAfterDatabaseFetch', $sql, $fetch);
return $fetch;
}
/**
* Fetches the first row of the SQL result.
*
* @param string $sql An SQL SELECT statement.
* @param array $bind Data to bind into SELECT placeholders.
* @return array
*/
public function fetchRow($sql, $bind = null)
{
if (Backend::$events) {
$fetch_event = Backend::$events->fireEvent('core:onBeforeDatabaseFetch', $sql);
if( isset($fetch_event[0]['result']) ) {
return $fetch_event[0]['result'];
}
}
$result = $this->query($this->prepare($sql, $bind));
$fetch = $this->driver()->fetch($result);
if (Backend::$events)
Backend::$events->fireEvent('core:onAfterDatabaseFetch', $sql, $fetch);
return $fetch;
}
Im still working on it but as you can see we do it on the fetch requests so that we have the $sql query still and can pass in a dataset. This effectively allows me to return a custom dataset for a query.