Problem with DboSource - Fatal error: Access to undeclared static property: DboSource::$methodCache

2011-11-04 Thread J.W.F. Thirion
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]))
 

Problem with DboSource - Fatal error: Access to undeclared static property: DboSource::$methodCache

2011-10-21 Thread J.W.F. Thirion
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.0 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

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:

Index: /var/www/html/myapp/lib/Cake/Model/Datasource/DboSource.php
===
--- /var/www/html/myapp/lib/Cake/Model/Datasource/DboSource.php
+++ /var/www/html/myapp/lib/Cake/Model/Datasource/DboSource.php
@@ -3087,6 +3087,9 @@
  */
public function __destruct() {
if ($this-_methodCacheChange) {
+   if (empty(self::$methodCache)) {
+   self::$methodCache =
Cache::read('method_cache', '_cake_core_');
+   }
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 {
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 a static variable, to only use one connection
per page-call (otherwise we would get a new handle every time a Model
is created)
static $_useDbConfig;
if (!isset($_useDbConfig)) {
// Get all available database-configs
$sources_list =
ConnectionManager::enumConnectionObjects();

// Find the slaves we have
$slaves = array();
foreach ($sources_list as $name = $values) {
// Slaves have to be named slave1,
slave2, etc...
if (preg_match('/^slave[0-9]*+$/i',
$name) == 1) {
$slaves[] = $name;
}
}

if (count($slaves)  0) {
// Randomly use a slave
$_useDbConfig = $slaves[rand(0,
count($slaves) - 1)];
} else {
$_useDbConfig = default;
}
}

$this-useDbConfig = $_useDbConfig;
parent::__construct($id, $table, $ds);
}

function save($data = null, $validate = true, $fieldList =
array()) {
$oldDb = $this-useDbConfig;
$this-setDataSource(master);
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;
}

function saveAll($data = null, $options = array()) {
$oldDb = $this-useDbConfig;
$this-setDataSource(master);
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, $options);
$this-setDataSource($oldDb);
return $isSave;
}

function updateAll($fields, $conditions=true) {
$oldDb = $this-useDbConfig;
$this-setDataSource(master);
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;
}

function delete($id = null, $cascade = true) {
$oldDb = $this-useDbConfig;
$this-setDataSource(master);
$isDeleted = parent::delete($id, $cascade);

Re: Problem with DboSource - Fatal error: Access to undeclared static property: DboSource::$methodCache

2011-10-21 Thread J.W.F. Thirion
Hi there,

Looks like my fix didn't work. I tried:


Index: /var/www/html/myapp/lib/Cake/Model/Datasource/DboSource.php
===
--- /var/www/html/myapp/lib/Cake/Model/Datasource/DboSource.php
+++ /var/www/html/myapp/lib/Cake/Model/Datasource/DboSource.php
@@ -3087,7 +3087,9 @@
  */
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_');
+   }
}
}

and this seems better, for now... :-)

Kind regards,
Derik

On Thu, 2011-10-20 at 23:11 -0700, J.W.F. Thirion wrote:

 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.0 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
 
 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:
 
 Index: /var/www/html/myapp/lib/Cake/Model/Datasource/DboSource.php
 ===
 --- /var/www/html/myapp/lib/Cake/Model/Datasource/DboSource.php
 +++ /var/www/html/myapp/lib/Cake/Model/Datasource/DboSource.php
 @@ -3087,6 +3087,9 @@
   */
 public function __destruct() {
 if ($this-_methodCacheChange) {
 +   if (empty(self::$methodCache)) {
 +   self::$methodCache =
 Cache::read('method_cache', '_cake_core_');
 +   }
 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


-- 
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
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php