Dear Community,
Not sure if this is the right place, but here goes. I've upgraded an
application from 1.3.10 to 2.0.2 and now get the following error:
"Fatal error: Access to undeclared static property: DboSource::
$methodCache in /var/www/html/myapp/lib/Cake/Model/Datasource/
DboSource.php on line 3090"
The method in my controller is accessed through an Ajax request (to
check if a user is still logged in).
I'm using a master-slave setup and have modified the AppModel
callbacks appropriately (as explained on one of the bakery pages). For
Cake 1.3 this works nicely, but on 2.0 I get the error above. I have
modified the cake code (I know I shouldn't :-) ) as follows:
--- cakephp.old/lib/Cake/Model/Datasource/DboSource.php
+++ cakephp.new/lib/Cake/Model/Datasource/DboSource.php
@@ -3096,7 +3096,7 @@ class DboSource extends DataSource {
*/
public function __destruct() {
if ($this->_methodCacheChange) {
- Cache::write('method_cache', self::$methodCache,
'_cake_core_');
+ if (!empty(self::$methodCache))
Cache::write('method_cache', self::
$methodCache, '_cake_core_');
}
}
This solved the problem and I no longer get the messages. Is this a
bug and is my fix above the correct solution?
Many thanks in advance!
Kind regards,
Derik
app/Model/AppModel.php
<?php
class AppModel extends Model {
private $useDbMaster = 'default';
private $useDbSlave = 'default';
public function __construct($id = false, $table = null, $ds =
null) {
// If a datasource is set via params, use it and
return.
if ((is_array($id) && isset($id['ds'])) || $ds) {
parent::__construct($id, $table, $ds);
return;
}
// Use static variables, to only use one connection
per page-call (otherwise we would get a new handle every time a Model
is created).
static $_useDbConfigMaster;
static $_useDbConfigSlave;
// Execute the following if either the master or the
slave config has not been set yet.
if ((!isset($_useDbConfigMaster)) || (!
isset($_useDbConfigSlave))) {
// Get all available database configurations.
$sources_list =
ConnectionManager::enumConnectionObjects();
// Find the masters and slaves we have.
$masters = array();
$slaves = array();
foreach ($sources_list as $name => $values) {
// Check for masters if one has not
been set yet, named 'master1', 'master2', etc.
if ((!isset($_useDbConfigMaster)) &&
(preg_match('/^master[0-9]*+$/i', $name) == 1))
$masters[] = $name;
// Check for slaves if one has not
been set yet, named 'slave1', 'slave2', etc.
if ((!isset($_useDbConfigSlave)) &&
(preg_match('/^slave[0-9]*+$/i', $name) == 1))
$slaves[] = $name;
}
// Update the master config if we found one
and we haven't set one already (choose randomly).
if ((!isset($_useDbConfigMaster)) &&
(count($masters) > 0))
$_useDbConfigMaster = $masters[rand(0,
count($masters) - 1)];
// Update the slave config if we found one and
we haven't set one already (choose randomly).
if ((!isset($_useDbConfigSlave)) &&
(count($slaves) > 0))
$_useDbConfigSlave = $slaves[rand(0,
count($slaves) - 1)];
}
// Set the master config or use the default if one
could not be found.
if (isset($_useDbConfigMaster))
$this->useDbMaster = $_useDbConfigMaster;
else
$_useDbConfigMaster = $this->useDbMaster;
// Set the slave config or use the default if one
could not be found.
if (isset($_useDbConfigSlave))
$this->useDbSlave = $_useDbConfigSlave;
else
$_useDbConfigSlave = $this->useDbSlave;
// The default will be the slave config, used for read-
only operations.
$this->useDbConfig = $this->useDbSlave;
parent::__construct($id, $table, $ds);
}
public function save($data = null, $validate = true,
$fieldList = array()) {
$oldDb = $this->useDbConfig;
$this->setDataSource($this->useDbMaster);
if (isset($this->data) && isset($this->data[$this-
>name]))
unset($this->data[$this->name]['modified']);
if (isset($data) && isset($data[$this->name]))
unset($data[$this->name]['modified']);
$isSave = parent::save($data, $validate, $fieldList);
$this->setDataSource($oldDb);
return $isSave;
}
public function saveAll($data = null, $options = array()) {
$oldDb = $this->useDbConfig;
$this->setDataSource($this->useDbMaster);
if (isset($this->data) && isset($this->data[$this-
>name]))
unset($this->data[$this->name]['modified']);
if (isset($data) && isset($data[$this->name]))
unset($data[$this->name]['modified']);
$isSave = parent::saveAll($data, $options);
$this->setDataSource($oldDb);
return $isSave;
}
public function updateAll($fields, $conditions = true) {
$oldDb = $this->useDbConfig;
$this->setDataSource($this->useDbMaster);
if (isset($this->data) && isset($this->data[$this-
>name]))
unset($this->data[$this->name]['modified']);
if (isset($data) && isset($data[$this->name]))
unset($data[$this->name]['modified']);
$isUpdated = parent::updateAll($fields, $conditions);
$this->setDataSource($oldDb);
return $isUpdated;
}
public function delete($id = null, $cascade = true) {
$oldDb = $this->useDbConfig;
$this->setDataSource($this->useDbMaster);
$isDeleted = parent::delete($id, $cascade);
$this->setDataSource($oldDb);
return $isDeleted;
}
public function deleteAll($conditions, $cascade = true,
$callbacks = false) {
$oldDb = $this->useDbConfig;
$this->setDataSource($this->useDbMaster);
$isDeleted = parent::deleteAll($conditions, $cascade,
$callbacks);
$this->setDataSource($oldDb);
return $isDeleted;
}
public function current_user() {
App::uses('Session', 'Component');
$Session = new SessionComponent(new
ComponentCollection());
return array('id'=>$Session->read('Authacl.id'));
}
}
?>
and my app/Config/database.php:
class DATABASE_CONFIG {
/* default = master, e.g. for sessions and if you don't have
app/views/pages/home.ctp you need it too */
var $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'port' => '',
'login' => 'root',
'password' => '',
'database' => 'mydb',
'schema' => '',
'prefix' => '',
'encoding' => ''
);
var $master = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'port' => '',
'login' => 'root',
'password' => '',
'database' => 'mydb',
'schema' => '',
'prefix' => '',
'encoding' => ''
);
/* slaves, can also be slave1, slave2, ... and then random one
will be chosen - read only */
var $slave = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'port' => '',
'login' => 'root',
'password' => '',
'database' => 'mydb',
'schema' => '',
'prefix' => '',
'encoding' => ''
);
}
--
Our newest site for the community: CakePHP Video Tutorials
http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others
with their CakePHP related questions.
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php