0

I'm using Cake's find('all') method in the controller to query a table I have, and the requestAction method to use the controller function (and so to output the data) within the view. The problem is that the find method is returning empty arrays(ouput is 'ArrayArrayArray'). The table is named 'users' (as per Cake conventions) and the code for all three files are below:

Code for the view (test.ctp)

<?php
$users = $this->requestAction('/users/read');
foreach($users as $user) {
     echo $user;
}
?>

Code for the controller (users_controller.php)

<?php
 class UsersController extends AppController {

         var $name = 'Users';

  function read(){
   $users = $this->User->find('all');
   if(isset($this->params['requested'])) {
    return $users;
   } 
  }
 }
?>

Code for the model (user.php)

<?php

    class User extends AppModel {
        var $name = 'User';
    }

?>

I've dwindled it down and discovered that $users is being set to NULL by the $this->User->find('all') statement because that statement is actually not returning any values. I know Cake is finding the table because of a typo I had earlier with the table name ('usres' instead of 'users'). I can't figure out what I'm missing or what I'm doing wrong. Would love some help. :)

2 Answers 2

1

The ouput is 'ArrayArrayArray' doen't means itz empty

try

foreach($users as $user) { 
debug($user); 
}
Sign up to request clarification or add additional context in comments.

2 Comments

You could also use pr($user). @Brady - You can't use echo to print an array.
Wow. Yea, I definitely feel naive now. Thanks for the help, guys. Merry Christmas to ya!
0

For me this code is meaningless.

if you want to use requestAction() use it in the proper way. It's used to call (and print) the parts of the application in the view.

Here is how to achieve the working and correct code:

Your Users class:

class Users extends AppController {
   function read(){
      $this->set('users', $this->User->find('all'));
      $this->layout = "ajax";
      $this->render();
   }
}

Your read.ctp:

<ul><?php
if(isset($users)){
   foreach($users as $user) {
     echo '<li>'.$user.'</li>';
   }
}
?></ul>

Now in your test.ctp you need to call:

<?php echo $this->requestAction(
                  array(
                        'controller'=>'users', 
                        'action'=>'read'
                  )
);?>

And you will see the result in your test action as unsorted list.

Comments

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.