使用 kohanna 模型和搜索功能减少 if/else 语句
Reduce if/else statements with kohanna model and search functionality
我有一个跨越 2000 多行的 if/else 块用于高级搜索功能,我觉得它可以被严重削减
这是大型 if/else 语句的一小部分示例
if($isSearch){
//Is search
if($isWordSearch){
//Is word search
if($isSubjectSearch){
//Is subject search
if($isDepartmentSearch){
//Is department Search
if($isOperatorSearch){
//Is operator search
if($isCustomerSearch){
//Is customer search
if($search_status == "Open"){
//Is word,subject,department,operator,customer,open
$tickets_row = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->and_where("user_id","=",$search_customer)
->and_where("status_id","NOT IN",array("50","60"))
->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
$tickets_row_count = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->and_where("user_id","=",$search_customer)
->and_where("status_id","NOT IN",array("50","60"))
->count_all();
}elseif($search_status == "Closed"){
//Is word,subject,department,operator,customer,closed
$tickets_row = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->and_where("user_id","=",$search_customer)
->and_where("status_id","IN",array("50","60"))
->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
$tickets_row_count = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->and_where("user_id","=",$search_customer)
->and_where("status_id","IN",array("50","60"))
->count_all();
}else{
//Is word,subject,department,operator,customer,all
$tickets_row = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->and_where("user_id","=",$search_customer)
->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
$tickets_row_count = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->and_where("user_id","=",$search_customer)
->count_all();
}
}else{
//Is not customer search
if($search_status == "Open"){
//Is word,subject,department,operator,open
$tickets_row = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->and_where("status_id","NOT IN",array("50","60"))
->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
$tickets_row_count = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->and_where("status_id","NOT IN",array("50","60"))
->count_all();
}elseif($search_status == "Closed"){
//Is word,subject,department,operator,closed
$tickets_row = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->and_where("status_id","IN",array("50","60"))
->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
$tickets_row_count = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->and_where("status_id","IN",array("50","60"))
->count_all();
}else{
//Is word,subject,department,operator,all
$tickets_row = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
$tickets_row_count = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->count_all();
}
}
}else{
//Is not operator search
if($isCustomerSearch){
//Is customer search
if($search_status == "Open"){
//Is word,subject,department,customer,open
$tickets_row = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("user_id","=",$search_customer)
->and_where("status_id","NOT IN",array("50","60"))
->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
$tickets_row_count = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("user_id","=",$search_customer)
->and_where("status_id","NOT IN",array("50","60"))
->count_all();
}elseif($search_status == "Closed"){
//Is word,subject,department,customer,closed
$tickets_row = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("user_id","=",$search_customer)
->and_where("status_id","IN",array("50","60"))
->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
$tickets_row_count = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("user_id","=",$search_customer)
->and_where("status_id","IN",array("50","60"))
->count_all();
}else{
//Is word,subject,department,customer,all
$tickets_row = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("user_id","=",$search_customer)
->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
$tickets_row_count = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("user_id","=",$search_customer)
->count_all();
}
}
这里发生的事情是我正在检查可能发生的每一种可能的搜索组合
我们是在搜索 word:subject:operator:customer:open
还是在搜索 word:subject:operator:customer:closed
所以我写出了所有可能的组合
在此之前,我曾尝试通过尝试
使它更加动态和优化
<?php
$tickets_row = $support_tickets_model;
if(true){
$tickets_row->where("subject","LIKE","%$search_text%");
}
$tickets_row
->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
?>
这只会导致 $tickets_row
为 null
将其简化为可维护格式的理想方法是什么?
毕竟,它可以非常动态地完成。
if($search_text) {
$support_tickets_model->where("subject","LIKE","%$search_text%");
}
if($search_status == "Closed")
$support_tickets_model->where("status_id","IN",array("50","60"));
else if($search_status == "Open")
$support_tickets_model->where("status_id","NOT IN",array("50","60"));
if($search_department)
$support_tickets_model->where("___dep_id","=",$search_department);
if($search_operator)
$support_tickets_model->where("operator_id","=",$search_operator);
if($search_customer)
$support_tickets_model->where("user_id","=",$search_customer);
//... another
//I'm not sure if you should clone the $support_tickets_model here before using `count_all()`
$tickets_row_count = $support_tickets_model->count_all();
$tickets_row = $support_tickets_model->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
我有一个跨越 2000 多行的 if/else 块用于高级搜索功能,我觉得它可以被严重削减
这是大型 if/else 语句的一小部分示例
if($isSearch){
//Is search
if($isWordSearch){
//Is word search
if($isSubjectSearch){
//Is subject search
if($isDepartmentSearch){
//Is department Search
if($isOperatorSearch){
//Is operator search
if($isCustomerSearch){
//Is customer search
if($search_status == "Open"){
//Is word,subject,department,operator,customer,open
$tickets_row = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->and_where("user_id","=",$search_customer)
->and_where("status_id","NOT IN",array("50","60"))
->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
$tickets_row_count = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->and_where("user_id","=",$search_customer)
->and_where("status_id","NOT IN",array("50","60"))
->count_all();
}elseif($search_status == "Closed"){
//Is word,subject,department,operator,customer,closed
$tickets_row = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->and_where("user_id","=",$search_customer)
->and_where("status_id","IN",array("50","60"))
->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
$tickets_row_count = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->and_where("user_id","=",$search_customer)
->and_where("status_id","IN",array("50","60"))
->count_all();
}else{
//Is word,subject,department,operator,customer,all
$tickets_row = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->and_where("user_id","=",$search_customer)
->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
$tickets_row_count = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->and_where("user_id","=",$search_customer)
->count_all();
}
}else{
//Is not customer search
if($search_status == "Open"){
//Is word,subject,department,operator,open
$tickets_row = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->and_where("status_id","NOT IN",array("50","60"))
->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
$tickets_row_count = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->and_where("status_id","NOT IN",array("50","60"))
->count_all();
}elseif($search_status == "Closed"){
//Is word,subject,department,operator,closed
$tickets_row = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->and_where("status_id","IN",array("50","60"))
->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
$tickets_row_count = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->and_where("status_id","IN",array("50","60"))
->count_all();
}else{
//Is word,subject,department,operator,all
$tickets_row = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
$tickets_row_count = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->count_all();
}
}
}else{
//Is not operator search
if($isCustomerSearch){
//Is customer search
if($search_status == "Open"){
//Is word,subject,department,customer,open
$tickets_row = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("user_id","=",$search_customer)
->and_where("status_id","NOT IN",array("50","60"))
->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
$tickets_row_count = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("user_id","=",$search_customer)
->and_where("status_id","NOT IN",array("50","60"))
->count_all();
}elseif($search_status == "Closed"){
//Is word,subject,department,customer,closed
$tickets_row = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("user_id","=",$search_customer)
->and_where("status_id","IN",array("50","60"))
->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
$tickets_row_count = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("user_id","=",$search_customer)
->and_where("status_id","IN",array("50","60"))
->count_all();
}else{
//Is word,subject,department,customer,all
$tickets_row = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("user_id","=",$search_customer)
->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
$tickets_row_count = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("user_id","=",$search_customer)
->count_all();
}
}
这里发生的事情是我正在检查可能发生的每一种可能的搜索组合
我们是在搜索 word:subject:operator:customer:open
还是在搜索 word:subject:operator:customer:closed
所以我写出了所有可能的组合
在此之前,我曾尝试通过尝试
使它更加动态和优化<?php
$tickets_row = $support_tickets_model;
if(true){
$tickets_row->where("subject","LIKE","%$search_text%");
}
$tickets_row
->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
?>
这只会导致 $tickets_row
为 null
将其简化为可维护格式的理想方法是什么?
毕竟,它可以非常动态地完成。
if($search_text) {
$support_tickets_model->where("subject","LIKE","%$search_text%");
}
if($search_status == "Closed")
$support_tickets_model->where("status_id","IN",array("50","60"));
else if($search_status == "Open")
$support_tickets_model->where("status_id","NOT IN",array("50","60"));
if($search_department)
$support_tickets_model->where("___dep_id","=",$search_department);
if($search_operator)
$support_tickets_model->where("operator_id","=",$search_operator);
if($search_customer)
$support_tickets_model->where("user_id","=",$search_customer);
//... another
//I'm not sure if you should clone the $support_tickets_model here before using `count_all()`
$tickets_row_count = $support_tickets_model->count_all();
$tickets_row = $support_tickets_model->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();