Reformulate the question:
I have a database with several tables and I want to make a query where I imply several. The problem is that he tells me that one of the columns does not find it. It is not from the "main" table but from one of the ones linked to it through a join. The column you can not find is "cr.programmes_level_id", exactly when you go through the line
$where .= " AND cr.programmes_level_id = ${level_id} ";
inside the if (at the end).
The error is:
SQLSTATE [42S22]: Column not found: 1054 Unknown column 'cr.programmes_level_id' in 'where clause'
Deputy code:
' $join_provinces = ' ( SELECT provinces.id AS id_province, provinces.name AS province_name, provinces.community_id AS community_id, communities.name AS community_name, communities.logo_file AS community_logo_file FROM provinces JOIN communities ON communities.id = provinces.community_id ORDER BY provinces.name ASC ) ';
$join_programmes = ' ( SELECT DISTINCT programmes_level_id, centre_id FROM centres_requests JOIN centres ON centres.id=centres_requests.centre_id GROUP BY centres_requests.centre_id) ';
$qb = $this->db->createQueryBuilder();
$qb
->select('c.id AS id', 'c.name AS name', 'cif', 'address', 'zip_code', 'city', 'province_id', 'province_name', 'community_id', 'community_name', 'community_logo_file', 'cm.name AS comunity_manager_name', 'email', 'phone', 'fax', 'underground', 'director_name', 'director_email',
'incorporation_year', 'kids_incorporation_year', 'beda_coordinator_name', 'beda_coordinator_email', 'beda_coordinator_mobile',
'examiner_centre', 'exams_coordinator_name', 'exams_coordinator_email', 'assistants_coordinator_name', 'assistants_coordinator_email', 'extracurricular_coordinator_name', 'extracurricular_coordinator_email', 'observations',
'c.logo_file AS logo_file', 'assistants_number', 'is_cam_bilingual', 'cam_bilingual_year', 'cam_bilingual_year_secondary', 'centre_type', 'beda_kids_coordinator_name', 'beda_kids_coordinator_email', 'community_manager_id',
'educational_levels_candidates', 'educational_levels_candidates_total', 'educational_levels_agreement ' )
->from('centres', 'c')
->join(
'c',
$join_provinces,
'provinces_data',
'provinces_data.id_province = c.province_id' )
->join(
'c',
'communities',
'cm',
'cm.id = c.community_manager_id' )
->join(
'c',
$join_programmes,
'cr',
'cr.centre_id = c.id');
$search = '%'.$params['search'].'%';
$search_params = [];
$where = " logical_delete = ${estado_centro} ";
if(!empty($level_id)){
$where .= " AND cr.programmes_level_id = ${level_id} ";
}
'
THANKS