I have three models: Project, Student, and Vote. The following code is the relationship between them. In the Project model:
public $hasMany = array('Student' => array(
'className' =>'Student',
'foreignKey'=>'project_id',
'order' => 'Student.studentName DESC',
),
'Vote' => array(
'className' =>'Vote',
'foreignKey'=>'project_id',
));
In the Student model:
public $belongsTo = array(
'Project'=>array(
'className'=>'Project',
'foreignKey'=>'project_id'
));
And in the Vote model:
public $belongsTo = array(
'Project'=>array(
'className' => 'Project',
'foreignKey' => 'id'
));
However, when I call this function (in the Project model):
public function getApprovedProjects(){
$approvedProjects = $this->find('all', array('conditions'=>array(
'Project.approved' => 1)));
return $approvedProjects;
}
This is the data that I get:
Array(
[0] => Array
(
[Project] => Array
(
[id] => 0
[projectName] => MyProject
[semester] => GAME
[description] => Test project
[imageLink] =>
[approved] => 1
)
[Student] => Array
(
)
[Vote] => Array
(
)
)
[1] => Array
(
[Project] => Array
(
[id] => 1
[projectName] => CRIA STUFF
[semester] => CRIA
[description] => Cria project
[imageLink] =>
[approved] => 1
)
[Student] => Array
(
)
[Vote] => Array
(
)
)
)
It doesn't find any of the students or votes associated with this Project, even though they exist and I think it should. Any idea what is going on?
Alright guys, I fixed the problem.
It turns out, Cake doesn't like doing associations with data that has an id of 0. All of my other fetches worked, and as soon as I changed all of the data in the table to have an id that wasn't 0, my find worked.
So, in the future, don't add entries to your table with an id of 0. Start at 1.