Re: Variable scope problem
I have a couple of suggestions - unless I am missing the point here.
Is category_id a field on the subcategories table? If so, presumably it's in $this->data, so why not just collect the value from there?
You can then use model validation to check it is populated. If it isn't, send the user back to the edit view with an appropriate error message. If it is there, this bunch of code:
App::import('Model','Category');
$cat = new Category();
$category = $cat->field('title', array('id' => $categoryID));
$this->set('category', $category);
..can be rewritten as follows:
$category = $this->Subcategory->Category->field('title', array('id' => $this->data['Subcategory']['category_id']));
I'd also say that you picking up the value of $category before checking for the presence of $id. If $id is missing you are redirecting out of there anyway; so move the $category code down a bit to save unnecessary work.
Jeremy Burns
Class Outfit
jeremyburns@classoutfit.com
(t) +44 (0) 208 123 3822
(m) +44 (0) 7973 481949
Skype: jeremy_burns
http://www.classoutfit.com
On 14 Feb 2011, at 06:28, adam_g2000 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.
>
> 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
> cake-php+unsubscribe@googlegroups.com 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
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home