Oh, just for grins, decided to try and automate this. I have a
Configurations controller where I do some admin stuff for the site,
and I added this function:
function counterCheck( )
{
$counterTemplate =
"UPDATE :atable AS :amodel
SET :counter = (
SELECT COUNT(:model.id)
FROM :table AS :model
WHERE :model.:foreignkey = :amodel.id
)";
$updates = array();
$models = Configure::listObjects('model') ;
foreach ($models as $model) {
$this->loadModel($model);
$mainTable = Inflector::tableize($model);
if (!empty($this->$model->belongsTo)) {
foreach ($this->$model->belongsTo as $assocModel =>
$def) {
if (isset($def['counterCache']) &&
$def['counterCache']) {
$assocTable =
Inflector::tableize($def['className']);
$assocClass = $def['className'];
$foreignKey = $def['foreignKey'];
$counter = strtolower($model) . '_count';
$query = String::insert($counterTemplate,
array(
'table' => $mainTable,
'model' => $model,
'atable' => $assocTable,
'amodel' => $assocModel,
'foreignkey' => $foreignKey,
'counter' => $counter
));
$result = $this->Configuration->query($query);
$affectedRows = $this->Configuration-
>getAffectedRows();
$updates[] = array(
'query' => $query,
'affected_rows' => $affectedRows
);
}
}
}
}
$this->set('updates', $updates);
} // end function Counter checks
The associated view just dumps the results in a table to display.
Seems to work to update the counters. The affectedRows lets me know
how many rows had their values changed, meaning something went wrong
somewhere in the database. Or, as is more likely the case since I'm
still developing this, I need to update the counters because I'm still
mucking around in the database some.
Check out the new CakePHP Questions site http://cakeqs.org and help others with
their CakePHP related questions.
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en