You need to specify your conditions as part of the find, or get
associated records from a Student model find.
I'd personally do this with the containable behavior.

Add the 'Containable' behavior to your models.
I normally add this to my AppModel (/app/app_model.php), as I use it everywhere.
Set $recursive to -1. Again, I do this on my app model:  public $recursive = -1;

In your StudentsController you can do the following:

$id = 2;
$student = $this->Student->find('all', array(
    'conditions' => array('Student.id' => $id),
    'contain' => array('ForeignSchool')
));

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


Now your data is available in $student in your view.
Your foreign schools are at: $student['ForeignSchool]

Hope that helps.

Cheers,
Graham Weldon (AKA: Predominant)



On Thu, Jun 3, 2010 at 2:51 PM, Roland Pish <[email protected]> wrote:
> Hello. I don't know if I'm doing things well, so I'll fire my
> question.
> I have a model relationship this way: Student hasMany ForeignSchool.
> First, I'll show my scenario, files and relevant code, then I'll
> explain the problem.
>
> SCENARIO:
> * Model file student.php (hasmany declaration):
> var $hasMany = array(
>
>        'ForeignSchool' => array(
>
>            'className' => 'ForeignSchool',
>
>            'foreignKey' => 'student_id',
>
>            'dependent' => true,
>
>        ),
>
>    );
>
> * Model file foreign_school.php (belongsto declaration):
> var $belongsTo = array(
>
>        'Student' => array(
>
>            'className' => 'Student',
>
>            'foreignKey' => 'student_id',
>
>        ),
>
>    );
>
> * Controller file students_controller.php (find call test):
> $this->Student->id = 2; //test hard code
> $foreignSchools = $this->Student->ForeignSchool->find('all');
>
> $this->set(compact('foreignSchools'));
>
>
> * And in the corresponding view file:
> print_r($foreignSchools);
>
> PROBLEM:
> The call to print_r($foreignSchools) is showing all the foreign
> schools but not the schools of the current Student. Is it something
> I'm missing that is avoiding the "filtering" of the foreign schools
> shown in the view?
>
> Thanks a lot!
>
> 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 [email protected]
> To unsubscribe from this group, send email to
> [email protected] 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 [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to