Reputation: 1059
Following is the association
Each school has many students. Each school belongs to a district
$this->table('school');
$this->belongsTo('Districts', [
'foreignKey' => 'district_id'
]);
$this->hasMany('Students', [
'foreignKey' => 'school_id'
]);
Let's say there are 4 districts. (Districts.id = 1,2,3,4) In School's Model, I'm trying to fetch the list of all students. But if the district is 1 or 2, I want to show only those students who scored more than 50 marks. And for the other districts, I am trying to show students who scored less than 35 marks. This is the finder function I wrote for the above mentioned.
public function findStudentsList(Query $query, array $options)
{
$query->select(['id','name','modified','created'])
->contain([
'Districts'=> function ($query) {
return $query->select(['id','name']);
},
'Students' => function ($query) {
return $query->where([
'CASE
WHEN Districts.id=1 THEN Students.marks >50
WHEN Districts.id=2 THEN Students.marks >50
ELSE Students.marks < 35
END']);
}
]);
}
But when I execute this, I get the following error.
Column not found: Unknown column 'Districts.id' in where clause
Is this the right way to do it? I'm new to CakePHP3.
Upvotes: 1
Views: 660