Hi.
I'm trying to override CakePHP's deleting of rows in the database
so it only looks like it's deleting them. So far I've done the
following:
* Updated all the tables except cake_sessions to include the column
`deleted` tinyint(1) unsigned NOT NULL default '0'
* Created the file app/app_model.php and entered the following (I
hope this may be useful to anyone else trying to do the same thing
later on, this is pretty generic code):
------------------------------------------------------------------------
<?php
class AppModel extends Model {
// Don't find anything that we've pretended to delete
function findAll($conditions = NULL, $fields = NULL, $order = NULL,
$limit = NULL, $page = 1, $recursive = NULL) {
if (is_array($conditions)) {
$not_deleted = array();
$conditions = $conditions + $not_deleted;
}
elseif (is_string($conditions)) {
$conditions .= " AND {$this->name}.deleted = FALSE";
}
elseif (is_null($conditions)) {
$conditions = "{$this->name}.deleted = FALSE";
}
return parent::findAll($conditions, $fields, $order, $limit,
$page,
$recursive);
}
function findCount($conditions = NULL) {
if (is_array($conditions)) {
$not_deleted = array();
$conditions = $conditions + $not_deleted;
}
elseif (is_string($conditions)) {
$conditions .= " AND {$this->name}.deleted = FALSE";
}
elseif (is_null($conditions)) {
$conditions = "{$this->name}.deleted = FALSE";
}
return parent::findCount($conditions);
}
function generateList($conditions = NULL, $order = NULL, $limit =
NULL, $keyPath = NULL, $valuePath = NULL, $groupPath = NULL) {
if (is_array($conditions)) {
$not_deleted = array();
$conditions = $conditions + $not_deleted;
}
elseif (is_string($conditions)) {
$conditions .= " AND {$this->name}.deleted = FALSE";
}
elseif (is_null($conditions)) {
$conditions = "{$this->name}.deleted = FALSE";
}
return parent::generateList($conditions, $order, $limit,
$keyPath,
$valuePath, $groupPath);
}
// Only pretend to delete data
function del($id) {
$this->create();
$this->read(NULL, $id);
$this->saveField('deleted', TRUE);
}
}
?>
------------------------------------------------------------------------
* Updated all the app/model/*.php files so that the links to other
tables all contain in the array key 'Conditions' and the array value
'[Table name].deleted = FALSE'
All of this works fine for pages that list single models, but for
the recersive ones, it's still getting rows that are linked to other
rows that in turn have been deleted.
For example, if I was bringing up a list of albums and which genre
they belonged to, it would be avoiding any deleted albums (good),
but showing all albums which belong to a deleted genre (not so good).
Am I overlooking something, or is this just correct behaviour on the
assumption that secondary information should always be pulled in?
I can update all the findAll, findCount and generateList lines to
work around this, but if it turned out I was missing something simple,
it'd be nice to fix it in the model area instead of the controller
one.
Thanks for your help everyone, and I hope my example code helps
anyone else trying to only pretend to delete data.
Zoe.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" 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
-~----------~----~----~----~------~----~------~--~---