0

I am trying to set a variable in my controller so that I can reference it in a view file

Here is an example of a successful set statement from the same action/function

//users_controller.php, index() function $this->set('bidBalance', $this->Membership->User->Point->balance($this->Auth->user('id'), true)); 

With the above, in my index.ctp file I can display the result by:

print($bidBalance); 

So I have to get some other data back to the view so I do the following:

$this->set('nextMembership', $this->Membership->find('first', array('conditions' => array('Membership.rank >' => $membership['Membership']['rank']), 'contain' => '', 'order' => array('Membership.rank' => 'asc')))); 

And when I try to access $nextMembership in my view file I get a notice:

Notice (8): Undefined variable: nextMembership [APP/views/themed/users/index.ctp, line 138 

The thing is the query I assign to 'nextMembership' is used elsewhere and successfully returns data so I am not sure what is wrong.

Also, for my sanity's sake I even tried

$this->set('nextMembership', 'hello world'); 

But I still get the undefined notice.

What am I doing wrong?

6
  • try to put your call result into a variable and debug the variable. Then use set on the variable not on the complete call. $nextMembership = $this->Membership->find('first', array('conditions' => array('Membership.rank >' => $membership['Membership']['rank']), 'contain' => '', 'order' => array('Membership.rank' => 'asc'))); debug($nextMembership); $this->set('nextMembership', $nextMembership); What does the debug output say? Commented Mar 5, 2015 at 16:22
  • Same outcome. And my last code line of set('nextMembership', 'hello world'); causes the notice too so there is something else going on Commented Mar 5, 2015 at 16:30
  • What does the debug($nextMembership); say inside the controller? Before you use "set". Commented Mar 5, 2015 at 16:34
  • I am new to Cake, I added that line after the assign, before the set, but nothing showed when I refreshed my view Commented Mar 5, 2015 at 16:36
  • Have you set your debugging level to 2? Look in the core.php. You should see at least an empty debug output. Commented Mar 5, 2015 at 16:45

1 Answer 1

2

First of all,if you are in a development mode,I recommends you to change the cakephp debug mode to level 2. You can do it on core.php file located in Config folder by changing Configure::write('debug', 0); to Configure::write('debug', 2); .After the changing, you can use debug($nextMembership) instead of print().It will give you better debugging results.

To confirms you query is correct,try to seperate the code like this format:

$nextMemberships = $this->Membership->find('first', array( 'conditions' => array('Membership.rank >' => $membership['Membership']['rank']), 'contain' => '', 'order' => array('Membership.rank' => 'asc'))); $this->set(compact('nextMemberships')); 

And in your View ,

place this at the top of the file : debug($nextMemberships) .

Hope this help.

Sign up to request clarification or add additional context in comments.

1 Comment

I would even put the debug in the controller, too. So andrewb can see if the variable is filled correctly in the controller and then check if it is transferred correctly into the view.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.