Assuming by 2nd if you refer to 

if ($this->Subcategory->save($this->data)) {
        //the save happens fine?
        //$this->Session->setFlash(__('The subcategory has been saved',
true));

        debug($categoryID);
        exit();
        //so you can see if there is fact anything up to this point in
$categoryID before you try redirecting

        
}

K

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf
Of adam_g2000
Sent: Monday, February 14, 2011 2:58 AM
To: CakePHP
Subject: Variable scope problem

The edit method in my controller is successfully being passed a
variable called $categoryID. I'm using the variable to query another
model, which happens successfully and the title field from the model
is displayed in the view via the variable defined here.

App::import('Model','Category');
$cat = new Category();
$category = $cat->field('title', array('id' => $categoryID));
$this->set('category', $category);

My problem arises in the second if statement in the method. By the
time I need the variable to pass through redirect it seems to have
fallen out of scope and I cannot figure out why - the variable appears
to be empty.

Here is the method.

        function edit($id = null, $categoryID = null) {
                App::import('Model','Category');
                $cat = new Category();
                $category = $cat->field('title', array('id' =>
$categoryID));
                $this->set('category', $category);
                if (!$id && empty($this->data)) {
                        $this->Session->setFlash(__('Invalid subcategory',
true));
                        $this->redirect(array('action' => 'index'));
                }
                if (!empty($this->data)) {
                        if ($this->Subcategory->save($this->data)) {
                                $this->Session->setFlash(__('The subcategory
has been saved',
true));
                                $this->redirect(array(
                                        'controller' => 'subcategories',
                                        'action' => 'index',
                                        $categoryID
                                ));
                        } else {
                                $this->Session->setFlash(__('The subcategory
could not be saved.
Please, try again.', true));
                        }
                }
                if (empty($this->data)) {
                        $this->data = $this->Subcategory->read(null, $id);
                }
                $categories = $this->Subcategory->Category->find('list');
                $resources = $this->Subcategory->Resource->find('list');
                $this->set(compact('categories', 'resources'));
        }

Please be gentle, I've spent a couple of months now reading the
cookbook and a textbook and a series of tutorials - though I am a
newbie! If anyone can offer any assistance and point out why this
might be I'd be very grateful. Thanks in advance.

Adam.

-- 
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
[email protected] For more options, visit this group at
http://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
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to