1

In my controller, I have a function (getRelatedStuff) that returns an array of values for use in my edit.ctp:

class MyController extends AppController {
    public function edit($id = null) {
        $this->getRelatedStuff($current['Form']['id'];
    }
    public function getRelatedStuff($id = null) {
        $this->loadModel('Related');
        $related = $this->Related->find('all', array(
            'conditions'=>array('Related.id' => $id)
        );
         return $this->set(compact('related'));
    }
}

This works fine when I use it in edit.ctp in a foreach loop.

foreach($related as $r) { 
    echo $r['Model']['field'];
}

However, I'm unable to use $related within my edit function itself, and I need access to that data for another purpose. Specifically, I need to set up a foreach() loop within function edit using $related. If I try to do so, or if I try debug $related within the edit function, I always get:

undefined variable $related in /docpath

What can I do to be able to use $related within my edit function?

2 Answers 2

2

You can use it in the template because of this line

$this->set(compact('related'));

(It's creating $related var for the template to use)

But your edit action in controller does not have access to $related variable, so you should return it:

class MyController extends AppController {
    public function edit($id = null) {
        $related = $this->getRelatedStuff($current['Form']['id']);
        var_dump($related)
    }
    public function getRelatedStuff($id = null) {
        $this->loadModel('Related');
        $related = $this->Related->find('all', array(
            'conditions'=>array('Related.id' => $id)
        );
        $this->set(compact('related'));
        return $related
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

But I'm already returning the variable. Look at the line again. return $this->set(compact('related'));
You're returning value of $this->set and I can't find in cackephp docs any info about what it returns. If it returns the set variable - then you need to assing this in your method $related = $this->getRelatedStuff($current['Form']['id']. But I really doubt $this->set works that way, since you can pass an array with many variables to set.
Controller::set() doesn't return anything.
0

I figured it out. I changed the function call in my edit function to the following:

$related = $this->getRelatedStuff(['Form']['id']);

I then set the variable for edit.ctp within the edit function:

$this->set('related', $related);

Lastly, I modified getRelatedStuff so that it only returned the raw variable, without setting it first:

return $related;

1 Comment

So basically you did what I suggested in my answer :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.