php - Join query in Cakephp missing results -
i'm having troubles cakephp join query. database built way : contact belongsto user
in contact controller, have fucntioin search contacts. contacts can searched user.first_name or user.last_name.
i built query :
$options['contain'] = array( 'user' => array( 'fields' => array('id', 'first_name', 'last_name'), ) ); $options['conditions']['or'] = array( array('contact.id like' => '%'.$search.'%'), array('user.first_name like' => '%'.$search.'%'), array('user.last_name like' => '%'.$search.'%'), array('building.title like' => '%'.$search.'%'), array('city.name like' => '%'.$search.'%'), array('manager.first_name like' => '%'.$search.'%'), array('manager.last_name like' => '%'.$search.'%'), ); $contacts = $this->contact->find('all', $options); this query give me sql query :
'select `contact`.`id`, `contact`.`specific_works`, `contact`.`user_id`, `user`.`id`, `user`.`first_name`, `user`.`last_name`, `pfre`.`contacts` `contact` left join `pfre`.`authake_users` `user` on (`contact`.`user_id` = `user`.`id`) `contact`.`white_label_id` = 18 , ((`contact`.`id` '%search%') or (`user`.`first_name` '%search%') or (`user`.`last_name` '%search%') or (`building`.`title` '%search%') or (`city`.`name` '%search%') or (`manager`.`first_name` '%search%') or (`manager`.`last_name` '%search%'))' this give me no results, whereas should 2 results (they in database).
does understand wrong in query ?
for information, complete (not simplified, fields) query :
'select `contact`.`id`, `contact`.`specific_works`, `contact`.`comments`, `contact`.`follow`, `contact`.`priority`, `contact`.`code`, `contact`.`step`, `contact`.`intervention_type`, `contact`.`intervention_precision`, `contact`.`energy_consumption_before`, `contact`.`energy_consumption_after`, `contact`.`ghg_emission_before`, `contact`.`ghg_emission_after`, `contact`.`diagnosis_date`, `contact`.`diagnostician`, `contact`.`heating_details`, `contact`.`heater_details`, `contact`.`equipments_details`, `contact`.`wall_details`, `contact`.`floor_details`, `contact`.`ceiling_details`, `contact`.`loan_received`, `contact`.`anah_project`, `contact`.`labor_cost`, `contact`.`equipment_cost`, `contact`.`other_cost`, `contact`.`cite_amount`, `contact`.`anah_amount`, `contact`.`cee_amount`, `contact`.`eco_amount`, `contact`.`tva_amount`, `contact`.`anah_ase_amount`, `contact`.`ptz_acquisition_amount`, `contact`.`anah_other_amount`, `contact`.`other_helps`, `contact`.`other_loans`, `contact`.`monthly_payments`, `contact`.`hide_consumptions`, `contact`.`abandonment_reason`, `contact`.`abandonment_reason_other`, `contact`.`project_source`, `contact`.`project_source_other`, `contact`.`renovation_type`, `contact`.`work_col_1`, `contact`.`work_col_2`, `contact`.`considered_energy`, `contact`.`considered_ges`, `contact`.`custom_1_energy`, `contact`.`custom_1_ges`, `contact`.`custom_2_energy`, `contact`.`custom_2_ges`, `contact`.`performed_energy`, `contact`.`performed_ges`, `contact`.`favorite_scenario1`, `contact`.`favorite_scenario2`, `contact`.`favorite_scenario3`, `contact`.`archived`, `contact`.`user_id`, `contact`.`manager_id`, `contact`.`white_label_id`, `contact`.`building_id`, `contact`.`ceie_id`, `contact`.`created`, `contact`.`modified`, `contact`.`rennes`, (energy_consumption_before - energy_consumption_after) `contact__energy_consumption_prevented`, (ghg_emission_before - ghg_emission_after) `contact__ghg_emission_prevented`, `user`.`id`, `user`.`first_name`, `user`.`last_name`, `manager`.`id`, `manager`.`first_name`, `manager`.`last_name`, `manager`.`email`, `manager`.`phone`, `building`.`id`, `building`.`title`, `building`.`city_id` `pfre`.`contacts` `contact` left join `pfre`.`authake_users` `user` on (`contact`.`user_id` = `user`.`id`) left join `pfre`.`authake_users` `manager` on (`contact`.`manager_id` = `manager`.`id`) left join `pfre`.`buildings` `building` on (`contact`.`building_id` = `building`.`id`) inner join `pfre`.`cities` `city` on (`building`.`city_id` = `city`.`id`) `contact`.`white_label_id` = 18 , ((`contact`.`id` '%search%') or (`user`.`first_name` '%search%') or (`user`.`last_name` '%search%') or (`building`.`title` '%search%') or (`city`.`name` '%search%') or (`manager`.`first_name` '%search%') or (`manager`.`last_name` '%search%'))' edit : result. depends on string use 'search', of them me right results.
i found problem ! when i'm building query, set join :
$options['joins'] = array( array( 'table' => 'cities', 'alias' => 'city', 'type' => 'inner', 'conditions' => [ 'building.city_id = city.id' ] ), ); some of buildings have no city_id. parameter don't include buildings no city_id or city_id not corresponding city.id.
i need know how include results no city_id.
thank helped me solution !
Comments
Post a Comment