The  results are perfectly correct for the code you are running; you are 
finding all Trackers. Your code:

$this->set('trackers', $this->User->Tracker->find('all'));

... is saying "go to the User model, then leap out to the Tracker model and 
bring me back all Trackers".

To filter the search, pass in a condition:

$this->set(
        'trackers',
        $this->User->Tracker->find(
                'all',
                array(
                        'conditions' = array(
                                'Tracker.user_id' => $userId
                                // where $userId is 'this' user
                        )
                )
        )
);

Or (in my opinion the better strategy) use the containable behaviour: 
http://book.cakephp.org/view/1323/Containable.

Jeremy Burns
Class Outfit

jeremybu...@classoutfit.com
http://www.classoutfit.com

On 17 Jun 2010, at 00:43, ryanl wrote:

> I'm in the process of creating my first web application with CakePHP,
> however I've run into some problems. I'll just start with the code:
> 
> 
> class User extends AppModel
> {
>       public $name = 'User';
>       public $belongsTo = 'Group';
>       public $hasMany = array(
>               'Tracker' => array(
>                       'className' => 'Tracker',
>                       'foreignKey' => 'user_id',
>                       'dependent' => true,
>                       'order' => 'Tracker.created DESC'));
> 
>       public $validate = array(
>               'username' => 'alphaNumeric',
>               'password' => 'notEmpty'
>       );
> }
> 
> 
> class Tracker extends AppModel
> {
>       public $name = 'Tracker';
>       public $belongsTo = 'User';
>       public $hasMany = array(
>               'Log' => array(
>                       'className' => 'Log',
>                       'foreignKey' => 'tracker_id',
>                       'dependent' => true,
>                       'order' => 'Log.created DESC'));
> 
>       public $validate = array(
>               'name' => 'alphaNumeric',
>       );
> }
> 
> 
> class Log extends AppModel
> {
>       public $name = 'Log';
>       public $belongsTo = 'Tracker';
>       public $validate = array();
> }
> 
> 
> Basically you can see that each user has a tracker, and each tracker
> has a log. The problem is that when I'm displaying trackers, I want to
> display for each user ONLY the trackers that he or she owns. But
> instead of doing that, it returns back all the trackers, including
> ones that the user doesn't own. This is the code that I'm using:
> 
> // inside of app/controllers/user_controller.php
> 
> $this->set('trackers', $this->User->Tracker->find('all'));
> $this->set('totalTrackers', $this->User->Tracker->find('count'));
> 
> 
> Can someone help me solve the problem?
> Thanks.
> 
> Check out the new CakePHP Questions site http://cakeqs.org and help others 
> with their CakePHP related questions.
> 
> You received this message because you are subscribed to the Google Groups 
> "CakePHP" group.
> To post to this group, send email to cake-php@googlegroups.com
> To unsubscribe from this group, send email to
> cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
> http://groups.google.com/group/cake-php?hl=en

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to