Thursday, February 24, 2011

Re: afterDelete not running as intended

Hi again Jeremy!

Thanks for that, there's my sanity check! I've done that, it's now
sitting in my Images Model. Unfortunately, nothing happens still....

Here's the entire model, am I still doing something stupid?

<?php
class images extends AppModel {
var $name = 'images';
var $validate = array(
'filename' => array(
'notempty' => array(
'rule' => array('notempty')
),
),
);

function afterDelete() {
// Re-sort orders to remove the 'hole'
// Create an array of the image table contents of IDs and Orders.
$conditions = array(
'fields' => array('id','order'),
'order' => 'Image.order ASC'
);
$images = $this->Image->find('all', $conditions);
//Loop through looking for order/count disparity and tidying up when
disparity is found.
$counter = 1;
foreach ($images as $image):
$image['Image']['order'] = $counter;
$this->Image->save($image, false, array('id','order'));
$counter++;
endforeach;
}
}
?>

On Feb 24, 7:22 pm, Jeremy Burns | Class Outfit
<jeremybu...@classoutfit.com> wrote:
> Is this in a model or a controller? I am guessing it's in a controller because you have a redirect statement. To make it run as afterDelete you need to move it into the model (afterDelete is a model callback) and then you'd need to remove the redirect statement because it will error.
>
> Jeremy Burns
> Class Outfit
>
> jeremybu...@classoutfit.comhttp://www.classoutfit.com
>
> On 24 Feb 2011, at 03:36, adam_g2000 wrote:
>
>
>
> > Hi Guys,
>
> > I have this method I've written...
>
> > function tidyup() {
> >    //Re-sort orders to remove the 'hole'
> >    // Create an array of the image table contents of IDs and Orders.
> >    $conditions = array(
> >                                    'fields' => array('id','order'),
> >                                    'order' => 'Image.order ASC'
> >    );
> >    $images = $this->Image->find('all', $conditions);
> >    //Loop through looking for order/count disparity and tidying up when
> > disparity is found.
> >    $counter = 1;
> >    foreach ($images as $image):
> >            if ($image['Image']['order'] != $counter) {
> >                    $image['Image']['order'] = $counter;
> >                    $this->Image->save($image, false, array('id','order'));
> >            }
> >            $counter++;
> >    endforeach;
> >    $this->redirect(array('action' => 'index'));
> > }
>
> > ...to reorder a list of Images in a database table once one is
> > deleted. This works perfectly.
>
> > However, what I really want to do is have it triggered automatically
> > once an Image is deleted.
>
> > So I would expect just changing function tidyup to function
> > afterDelete should work perfectly. When I do that, the function never
> > runs.
>
> > Can I get a sanity check please? Can anyone spot my error?
>
> > Thanks in advance for any offers of assistance.
>
> > --
> > Our newest site for the community: CakePHP Video Tutorialshttp://tv.cakephp.org
> > Check out the new CakePHP Questions sitehttp://ask.cakephp.organd help others with their CakePHP related questions.
>
> > To unsubscribe from this group, send email to
> > cake-php+unsubscribe@googlegroups.com For more options, visit this group athttp://groups.google.com/group/cake-php

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

No comments: