On Mon, Feb 14, 2011 at 1:28 AM, adam_g2000 <[email protected]> wrote:
> 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.
>

You have a couple of lines with,
$this->Subcategory->Category->find(...), which suggests that the two
models are already associated. So, there's no need to import Category
model earlier on. Get rid of the first two lines of the method.

Next, don't set() the 'category' viewVar at the top of the method. If
$data is available, and you then save and redirect, calling set() will
have been pointless. Put the set() line lower down so it's only called
if either $data is empty, or saving fails for some reason.

You say that $categoryID is empty. How did you determine that? Have
you placed any debugging code in there? Is it possible that this is
just a routing issue? Try making it explicit:

Router::connect(
        '/subcategories/:categoryID',   // for example
        array(
                'controller' => 'subcategories',
                'action' => 'index'
        ),
        array(
                'categoryID' => '[0-9]+',
                'pass' => array('categoryID')
        )
);

controller:
public function index($categoryID = null) { ... }

redirect:

$this->redirect(
        array(
                'controller' => 'subcategories',
                'action' => 'index',
                'categoryID' => $categoryID
        )
);

Finally, if I'm reading this correctly--that you've got both a
CategoriesController and a SubcategoriesController--I have to think
you're heading for very complicated times ahead. Stick with Categories
only. A SubCategory is merely a Category with a parent. You can still
have a hasMany association between Category and SubCategory as an
*alias* but the class should remain Category. The imaportant thing,
though, is to get rid of the SubcategoriesController, which will only
complicate matters and is bad design, in any case. Pass both the $id
and the $parent_id to the methods that need it. If the latter is null,
this is a top-level Category.

-- 
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