【发布时间】:2017-09-11 17:30:40
【问题描述】:
我在处理 Cakephp 连接查询时遇到问题。我的数据库是这样构建的:Contact belongsTo User
在我的联系人控制器中,我有一个搜索联系人的功能。可以通过 User.first_name 或 User.last_name 搜索联系人。
我这样构建查询:
$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);
这个查询给了我那个 sql 查询:
'SELECT `Contact`.`id`, `Contact`.`specific_works`, `Contact`.`user_id`, `User`.`id`, `User`.`first_name`, `User`.`last_name`, FROM `pfre`.`contacts` AS `Contact` LEFT JOIN `pfre`.`authake_users` AS `User` ON (`Contact`.`user_id` = `User`.`id`) WHERE `Contact`.`white_label_id` = 18 AND ((`Contact`.`id` LIKE '%search%') OR (`User`.`first_name` LIKE '%search%') OR (`User`.`last_name` LIKE '%search%') OR (`Building`.`title` LIKE '%search%') OR (`City`.`name` LIKE '%search%') OR (`Manager`.`first_name` LIKE '%search%') OR (`Manager`.`last_name` LIKE '%search%'))'
这没有给我任何结果,而我应该得到 2 个结果(它们在我的数据库中)。
有人明白这个查询有什么问题吗?
有关信息,完整(未简化,包含所有字段)查询:
'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) AS `Contact__energy_consumption_prevented`, (ghg_emission_before - ghg_emission_after) AS `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` FROM `pfre`.`contacts` AS `Contact` LEFT JOIN `pfre`.`authake_users` AS `User` ON (`Contact`.`user_id` = `User`.`id`) LEFT JOIN `pfre`.`authake_users` AS `Manager` ON (`Contact`.`manager_id` = `Manager`.`id`) LEFT JOIN `pfre`.`buildings` AS `Building` ON (`Contact`.`building_id` = `Building`.`id`) INNER JOIN `pfre`.`cities` AS `City` ON (`Building`.`city_id` = `City`.`id`) WHERE `Contact`.`white_label_id` = 18 AND ((`Contact`.`id` LIKE '%search%') OR (`User`.`first_name` LIKE '%search%') OR (`User`.`last_name` LIKE '%search%') OR (`Building`.`title` LIKE '%search%') OR (`City`.`name` LIKE '%search%') OR (`Manager`.`first_name` LIKE '%search%') OR (`Manager`.`last_name` LIKE '%search%'))'
编辑: 我有时会得到一些结果。这取决于我用作“搜索”的字符串,但其中一些可以让我得到正确的结果。
【问题讨论】:
-
这与cakephp无关,而是与执行的SQL有关,因此标签错误。您的查询包含错误。快速浏览一下,这部分有一个额外的
,。可能会有更多错误, 'User'.'last_name', FROM 'pfre'.'contacts' -
感谢您的回答,但这只是因为我删除了一些字段以使查询更具可读性。主题中添加了完整的查询。