我在Laravel 5.2
编写了一个查询,如下所示,我使用了范围。范围scopeSubscriberCriteriaSearch
具有进一步的子范围。方案是,如果主范围找到"first_name" "=" "test" OR "last_name" "!=" "demo"
等,则将调用相关的子范围。
$record = self::leftJoin("$customersTable as customers", 'customers.email', '=', "{$this->table}.email")
->leftJoin("$campaignsTable as campaigns", 'campaigns.campaign_id', '=', "{$this->table}.campaign_id")
->select("$this->primaryKey", "{$this->table}.first_name", "{$this->table}.last_name", "{$this->table}.email", "{$this->table}.phone", "{$this->table}.address", "campaigns.campaign_id", "campaigns.campaign_name", "customers.order_count")
->whereRaw($where);
$record->subscriberCriteriaSearch($criteria, $search_type, $persist)
->searchAfter($waiting_min, $search_type, $this->user_id)
->excludeLeadEmail($last_used_email)
->greaterCreateDateSearch($campaign_start_date)
->get();
=========== 主要范围如下:
public function scopeSubscriberCriteriaSearch($query, $criteria, $search_type, $persist = TRUE)
{
if (!empty($criteria))
{
$criteria_var = ['first_name', 'last_name', 'email', 'phone_number', 'city', 'country', 'state', 'zip', 'address', 'campaign', 'affiliate', 'subAffiliate', 'createdAt'];
return $query->where(function($sub_query) use ($criteria, $criteria_var, $search_type, $persist)
{
foreach ($criteria as $rule)
{
if (in_array($rule->condition, $criteria_var))
{
$position = array_search($rule->condition, $criteria_var);
($rule->condition == 'affiliate' || $rule->condition == 'subAffiliate') ? $sub_query->{$criteria_var[$position] . 'Search'}($rule->logical, $rule->input_val, $search_type, $persist) : $sub_query->{$criteria_var[$position] . 'Search'}($rule->logical, $rule->input_val, $persist);
}
}
});
}
}
子范围(例如:)如下:
public function scopeFirst_nameSearch($query, $operator, $first_name, $persist = TRUE)
{
$query_operator = $this->getQueryOperator($operator);
if (!empty($operator) && !empty($first_name))
{
$query_operator_val = preg_match('/like/i', $query_operator) ? ($operator == 'starts' ? '"' . $first_name . '%"' : ($operator == 'ends' ? '"%' . $first_name . '"' : '"%' . $first_name . '%"')) : '"' . $first_name . '"';
return $persist ? $query->where($this->table . ".first_name", $query_operator, DB::raw($query_operator_val)) : $query->orWhere($this->table . ".first_name", $query_operator, DB::raw($query_operator_val));
}
}
public function scopeLast_nameSearch($query, $operator, $last_name, $persist = TRUE)
{
$query_operator = $this->getQueryOperator($operator);
if (!empty($operator) && !empty($last_name))
{
$query_operator_val = preg_match('/like/i', $query_operator) ? ($operator == 'starts' ? '"' . $last_name . '%"' : ($operator == 'ends' ? '"%' . $last_name . '"' : '"%' . $last_name . '%"')) : '"' . $last_name . '"';
return $persist ? $query->where($this->table . ".last_name", $query_operator, DB::raw($query_operator_val)) : $query->orWhere($this->table . ".last_name", $query_operator, DB::raw($query_operator_val));
}
}
因此,如果$ persist为False,则这些子查询的orWhere部分将执行。但在我的情况下,查询现在生成如下:
选择prospect_id
,sem_1_prospects
。first_name
,sem_1_prospects
。last_name
,sem_1_prospects
。email
,{{1} }。sem_1_prospects
,phone
。sem_1_prospects
,address
。sem_campaigns
,campaign_id
。sem_campaigns
,campaign_name
。来自sem_customers
order_count
的{{1}} sem_1_prospects
sem_1_customers
sem_customers
sem_customers
email
。sem_1_prospects
左边加入email
上的sem_1_campaigns
为sem_campaigns
。sem_campaigns
= campaign_id
。sem_1_prospects
其中((campaign_id
。sem_1_prospects
= “test”)和(first_name
。sem_1_prospects
不喜欢“%demo%”));
有人可以帮我找出问题吗?