Tuesday, November 2, 2010

Re: Standard mechanism for telling why a record hasn't been deleted?

Hi,

Personally for these kinds of errors, I prefer to rely on the errors
raised by the database.

However, AFAIK, CakePHP does not return these errors automatically. I
found a solution, but it uses a very discussed technique: accessing
the Session in the model layer. It could also use a model variable
instead, that would be then read in the controller actions, but this
would mean updating a lot of them...

This is what it looks like:

app_controller
--------------------

function beforeFilter()
{
AppModel :: $cakeSession = $this->Session;
}


app_model
---------------

class AppModel extends Model
{
static $cakeSession = null;

function delete($id = null, $cascade = true)
{
$result = parent :: delete($id, $cascade);

$this->store_datasource_error();

return $result;
}

/*
* Override save() and saveAll() functions here as well if you want
*/

function store_datasource_error()
{
if(!empty($this->getDataSource()->error))
{
$this->validationErrors['datasource'] = $this-
>translate_datasource_error($this->getDataSource()->error);

AppModel :: $cakeSession->write('datasource_error', $this-
>validationErrors['datasource']);
}
}

function translate_datasource_error($error_msg)
{
if(stripos($error_msg, 'violates foreign key constraint) !==
false)
{
$error_msg = __('this item is referenced in the database',
true);
}
elseif(stripos($error_msg, 'duplicate key value violates
unique constraint') !== false)
{
$error_msg = __('this value already exists', true);
}
elseif( ... )
{
...
}

return $error_msg;
}
}


Layout
---------

<?php
if($this->Session->check('datasource_error'))
{
echo $this->Session->read('datasource_error');
$this->Session->delete('datasource_error');
}
?>

Kind regards,
nIcO

On 2 nov, 15:09, psybear83 <psybea...@gmail.com> wrote:
> Hi all layer
>
> Is there a standard mechanism for telling why a record hasn't been
> deleted?
>
> When a record isn't deleted, it just tells "Xxx was not deleted", but
> without any reason. So for example, when I'm creating a
> beforeDelete(..) hook that prevents the record from deletion, how
> should I let the user know why it hasn't been deleted?
>
> Would using a $deletionError variable be an acceptable way?
>
> function beforeDelete($cascade) {
>   if($this->hasDependentRecords()) {
>     $this->deletionError = 'record has dependent records';
>     return false;
>   }
>
> }
>
> And in the controller:
>
> if ($this->Post->delete($id)) {
>   ...} else {
>
>   $this->Session->setFlash('Post was not deleted because '.$this->Post-
>
> >deletionError);
> }
>
> What do you think about this solution? Is there a better way?
>
> Thanks, Josh

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 cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: