0

I have a hasMany relationship set up. User hasMany Projects. In my users controller in the profile view I can see the projects being returned for that user in the SQL dump.

I want to display each project in my profile view for that user. So I have set up a association between user and projects. User hasMany projects. I have created a profile action in the users controller with the associated profile view. When I navigate to the profile view of one of the users with, I can see that it is querying for the projects in the SQL dump.

This is the SQL dump with the correct userid:

SELECT `Project`.`id`, `Project`.`title`, `Project`.`created`, `Project`.`website`, `Project`.`language_id`, `Project`.`user_id`, `Project`.`description`, `Project`.`tags`, `Project`.`creator_id` FROM `projects` AS `Project` WHERE `Project`.`user_id` = (5)

How can I use the data from this query?

Project Model

var $belongsTo = array(
    'User' => array(
        'className'    => 'User',
        'foreignKey'    => 'user_id',
        'counterCache' => 'num_of_projects'
        ),
    'Language'
     );  
?>

User Model

  var $hasMany = array('Project' => 
                                array('className'     => 'Project', 
                                      'conditions'    => '',
                                      'order'         => '', 
                                      'limit'         => '',                    
                                      'foreignKey'    => 'user_id',              
                                      'dependent'     => true,                   
                                      'exclusive'     => false,                  
                                      'finderQuery'   => '',                      
                                      'fields'        => '',                      
                                      'offset'        => '',                      
                                      'counterQuery'  => ''
                                      )           
                    );

User_Controller's profile action

function profile($id = null) {
    $this->User->id = $id;
    $this->set('user', $this->User->read());
}

1 Answer 1

1

In your view

foreach($user['Project'] as $project)
{
   echo $project['title'].'<br />';
}

To see the bigger picture, just use pr(), you should be able to see how to access the rest of the data from there.

pr($user);
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for you reply, tried it but getting this error: invalid argument supplied for foreach(). Does it know what projects is? It's not set as it would be in the controller. It's via the association
Ok... can you confirm that we're not even involving views at the moment. You just want to loop over each 'project' in the 'users' controller? So you're doing something like: $users = $this->User->find(); and a user hasMany projects? If you post some code it would really help out, I'm just guessing here :D
I have updated my question, thanks for helping me out with this!
I've updated my answer. Keep in mind, that since CakePHP is just returning arrays you can just dump it out with print_r(), or cake's built in pr(), which makes it easier to read by wrapping it in html pre tags. That way, you should always be able to see what data you have, and how to access it.

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.