2013-08-07 32 views
-1

我有這樣的索引方法:Laravel 4方法的改進

public function index() 
{ 
    // In the view, there are several multiselect boxes (account managers, company names and account types). This code retrives the values from the POST method of the form/session. 
    $company_names_value = Input::get('company_names_value'); 
    $account_managers_value = Input::get('account_managers_value'); 
    $account_types_value = Input::get('account_types_value'); 

    // If there has been no form submission, check if the values are empty and if they are assign a default. 
    // Essentially, all of the records in the table column required. 
    if (is_null($company_names_value)) 
    { 
     $company_names_value = DB::table('accounts') 
      ->orderBy('company_name') 
      ->lists('company_name'); 
    } 

    if (is_null($account_managers_value)) 
    {  
     $account_managers_value = DB::table('users') 
      ->orderBy(DB::raw('CONCAT(first_name," ",last_name)')) 
      ->select(DB::raw('CONCAT(first_name," ",last_name) as amname')) 
      ->lists('amname'); 
    } 

    if (is_null($account_types_value)) 
    { 
     $account_types_value = DB::table('account_types') 
      ->orderBy('type') 
      ->lists('type'); 
    } 

    // In the view, there is a dropdown box, that allows the user to select the amount of records to show per page. Retrieve that value or set a default. 
    $perPage = Input::get('perPage', 10); 

    // This code retrieves the order from the session that has been selected by the user by clicking on a table column title. The value is placed in the session via the getOrder() method and is used later in the Eloquent query and joins. 
    $order = Session::get('account.order', 'company_name.asc'); 
    $order = explode('.', $order); 

    // Here we perform the joins required and order the records, then select everything from accounts and select their id's as aid. Then whereIn is used to select records where company name, account manager name and account type matches the values of the multiselect boxes or the default set above. 
    $accounts_query = Account::leftJoin('users', 'users.id', '=', 'accounts.user_id') 
     ->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id') 
     ->orderBy($order[0], $order[1]) 
     ->select(array('accounts.*', DB::raw('accounts.id as aid'))); 

    if (!empty($company_names_value)) $accounts_query = $accounts_query->whereIn('accounts.company_name', $company_names_value); 

    $accounts = $accounts_query->whereIn(DB::raw('CONCAT(users.first_name," ",users.last_name)'), $account_managers_value) 
     ->whereIn('account_types.type', $account_types_value) 
     ->paginate($perPage)->appends(array('company_names_value' => Input::get('company_names_value'), 'account_managers_value' => Input::get('account_managers_value'), 'account_types_value' => Input::get('account_types_value'))); 

    $accounts_trash = Account::onlyTrashed() 
     ->leftJoin('users', 'users.id', '=', 'accounts.user_id') 
     ->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id') 
     ->orderBy($order[0], $order[1]) 
     ->select(array('accounts.*', DB::raw('accounts.id as aid'))) 
     ->get(); 

    $message = Session::get('message'); 

    $default = ($perPage === null ? 10 : $perPage); 

    $this->layout->content = View::make('admin.accounts.index', array(
     'accounts'   => $accounts, 
     'accounts_trash' => $accounts_trash, 
     'company_names'  => DB::table('accounts')->orderBy('company_name')->lists('company_name', 'company_name'), 
     'account_managers' => DB::table('users')->orderBy(DB::raw('CONCAT(first_name," ",last_name)'))->select(DB::raw('CONCAT(first_name," ",last_name) as amname'))->lists('amname', 'amname'), 
     'account_types'  => DB::table('account_types')->orderBy('type')->lists('type', 'type'), 
     'perPage'   => $perPage, 
     'message'   => $message, 
     'default'   => $default 
    )); 
} 

基本上,我建立一個查詢,以搜索多個表(因此連接)。在視圖中,用戶可以從各種多選框中選擇多個值,然後提交表單,然後填充$ company_names_value,$ account_managers_value和$ account_types_value變量。

最初,當沒有表單提交時,我使用查詢生成器爲每種類型選擇所有記錄,然後在查詢中使用它們。

它的工作原理,但它是緩慢和凌亂。我想知道你們中的任何一位Laravel 4大師能夠幫助我進一步改進,以便查詢速度更快,代碼更輕。

在此先感謝。

+1

聖球!修理你的縮進 – afarazit

+0

@afarazit縮進是怎麼回事? –

回答

0

這現在已經重構了很多,現在速度非常快。我將大部分代碼移到了模型中,並重構了代碼。

這裏是新的索引方法:

public function index() 
{ 
    $account = explode(',', Input::get('account')); 
    $account_manager = explode(',', Input::get('account_manager')); 
    $account_type = explode(',', Input::get('account_type')); 

    $perPage = Input::get('perPage', 10); 

    $order = Session::get('account.order', 'company_name.asc'); 
    $order = explode('.', $order); 

    $accounts = Account::accounts($order, $account, $account_manager, $account_type)->paginate($perPage)->appends(array(
     'account'   => Input::get('account'), 
     'account_manager' => Input::get('account_manager'), 
     'account_type'  => Input::get('account_type'), 
     'perPage'   => Input::get('perPage') 
    )); 

    $accounts_trash = Account::accountsTrash($order)->get(); 

    $message = Session::get('message'); 

    $default = ($perPage === null ? 10 : $perPage); 

    $this->layout->content = View::make('admin.accounts.index', compact('accounts', 'accounts_trash', 'message', 'default')); 
} 

在我的控制器,這是我的AJAX調用過程中使用的新getAccountByName()方法。這也許應該去模型:

public function getAccountByName() 
{ 
    $name = Input::get('account'); 
    return Account::select(array('id', DB::raw('company_name as text')))->where('company_name', 'like', "%$name%")->get(); 
} 

而且在我的模型終於兩種新方法用於檢索賬戶和賬務垃圾:

public function scopeAccounts($query, $order, $account, $account_manager, $account_type) 
{ 
    $query->leftJoin('users', 'users.id', '=', 'accounts.user_id') 
     ->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id') 
     ->orderBy($order[0], $order[1]) 
     ->select(array('accounts.*', DB::raw('accounts.id as aid'))); 

    if (!empty($account[0])) { 
     $query = $query->whereIn('accounts.id', $account); 
    } 
    if (!empty($account_manager[0])) { 
     $query = $query->whereIn('users.id', $account_manager); 
    } 
    if (!empty($account_type[0])) { 
     $query = $query->whereIn('account_types.id', $account_type); 
    } 
} 

public function scopeAccountsTrash($query, $order) 
{ 
    $query->onlyTrashed() 
     ->leftJoin('users', 'users.id', '=', 'accounts.user_id') 
     ->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id') 
     ->orderBy($order[0], $order[1]) 
     ->select(array('accounts.*', DB::raw('accounts.id as aid'))); 
} 

再次,有可能是一噸的東西拿到這裏收拾但我更接近更快更清潔的解決方案。這樣做已經將加載時間從12秒減少到234ms。