簡體   English   中英

獲取數據庫信息的更好方法

[英]Better way to get database info

好吧,所以我有一個座位系統,它會生成一個30x30的網格,並且對於每個網格,數據庫中都有一行,但是當我在頁面上生成該網格時,我目前每個元素發出5個SQL請求( 900個元素),我們都知道這不是最優的。

為了渲染它,我做了一些檢查,並回顯了這段代碼:

<li class="seat" @if(Seat::showTitle($id) == 1) data-toggle="tooltip" data-placement="left" title="" data-original-title="{{ Seat::title($id) }}" @endif><a class="{{ Seat::getCSSClass($id) }}">@if(Seat::showTitle($id) == 1) {{ Seat::seatID($id) }} @endif</a></li>

這是Seat模型如何工作的示例

<?php

class Seat extends Eloquent {

    protected $fillable=array('user_id','timestamp','temp_user_id','temp_timestamp', 'class', 'seat_id', 'seat_name');

    protected $table = 'seats';

    public static $classes =    array(
                    "blank" => array(
                        "css_class"     => "seating_blank",
                        "can_reserve"   => 0,
                        "show_title"    => 0,
                        "title"         => "Blank"
                    ),
                    "available" => array(
                        "css_class"     => "seating_green",
                        "can_reserve"   => 1,
                        "show_title"    => 1,
                        "title"         => "Ledig"
                    ),
                    "reserved" => array(
                        "css_class"     => "seating_grey",
                        "can_reserve"   => 0,
                        "show_title"    => 1,
                        "title"         => "Reserveret"
                    ),
                    "taken" => array(
                        "css_class"     => "seating_red",
                        "can_reserve"   => 0,
                        "show_title"    => 1,
                        "title"         => "Betalt og Reserveret"
                    ),
                    "temp_taken" => array(
                        "css_class"     => "seating_orange",
                        "can_reserve"   => 0,
                        "show_title"    => 1,
                        "title"         => "Reservation igangsat"
                    )

                );


    public static function getCSSClass($seat) 
    {
        $theSeat = Self::where('id', '=', $seat)->first();

        if($theSeat) {
            $class = $theSeat->class;

            return Self::$classes[$class]['css_class'];
        } else {
            return NULL;
        }
    }

    public static function canReserve($seat) 
    {
        $theSeat = Self::where('id', '=', $seat)->first();

        if($theSeat) {
            $class = $theSeat->class;

            return Self::$classes[$class]['can_reserve'];
        } else {
            return NULL;
        }
    }

    public static function showTitle($seat) 
    {
        $theSeat = Self::where('id', '=', $seat)->first();

        if($theSeat) {
            $class = $theSeat->class;

            return Self::$classes[$class]['show_title'];
        } else {
            return NULL;
        }
    }
    public static function title($seat) 
    {
        $theSeat = Self::where('id', '=', $seat)->first();

        if($theSeat) {
            $class = $theSeat->class;

            return Self::$classes[$class]['title'];
        } else {
            return NULL;
        }
    }

    public static function seatID($seat)
    {
        $theSeat = Self::where('id', '=', $seat)->first();

        if($theSeat) {
            return $theSeat->seat_id;
        } else {
            return NULL;
        }
    }

    public static function seatName($seat)
    {
        $theSeat = Self::where('id', '=', $seat)->first();

        if($theSeat) {
            return $theSeat->seat_name;
        } else {
            return NULL;
        }
    }

    public static function classTitle($class)
    {
        return Self::$classes[$class]["title"];
    }

    public static function userID($seat)
    {
        $theSeat = Self::where('id', '=', $seat)->first();

        if($theSeat) {
            return $theSeat->user_id;
        } else {
            return NULL;
        }
    }

    public static function getClass($seat)
    {
        $theSeat = Self::where('id', '=', $seat)->first();

        if($theSeat) {
            return $theSeat->class;
        } else {
            return NULL;
        }
    }


}

每個請求有5個這樣的功能,這就是很多方法。 因此,我需要一些好的方法來最小化該系統的SQL負載。

您的模型應如下所示:

class Seat extends Eloquent {

    protected $fillable = array('user_id','timestamp','temp_user_id','temp_timestamp', 'class', 'seat_id', 'seat_name');

    protected $table = 'seats';

    private $classes =  array(
                    "blank" => array(
                        "css_class"     => "seating_blank",
                        "can_reserve"   => 0,
                        "show_title"    => 0,
                        "title"         => "Blank"
                    ),
                    "available" => array(
                        "css_class"     => "seating_green",
                        "can_reserve"   => 1,
                        "show_title"    => 1,
                        "title"         => "Ledig"
                    ),
                    "reserved" => array(
                        "css_class"     => "seating_grey",
                        "can_reserve"   => 0,
                        "show_title"    => 1,
                        "title"         => "Reserveret"
                    ),
                    "taken" => array(
                        "css_class"     => "seating_red",
                        "can_reserve"   => 0,
                        "show_title"    => 1,
                        "title"         => "Betalt og Reserveret"
                    ),
                    "temp_taken" => array(
                        "css_class"     => "seating_orange",
                        "can_reserve"   => 0,
                        "show_title"    => 1,
                        "title"         => "Reservation igangsat"
                    )

                );

    public static function getSeats() 
    {
        $seats = $this->all(); //get all seats you may want to add some where clause
        return $this->prepareData($seats);
    }

    private function prepareData($seats) 
    {
        foreach ($seats as $key => $seat) 
        {
            $seats[$key]->css_class = $this->classes[$seat->class]['css_class'];
            $seats[$key]->can_reserve = $this->classes[$seat->class]['can_reserve'];
            $seats[$key]->show_title = $this->classes[$seat->class]['show_title'];
            $seats[$key]->title = $this->classes[$seat->class]['title'];
        }
        return $seats;
    }

}

鑒於您想要執行以下操作:

@foreach ($seats as $seat)
   <li class="seat" 
        @if($seat->show_title) data-toggle="tooltip" data-placement="left" title="" data-original-title="{{ $seat->title }}" @endif>
        <a class="{{ $seat->css_class }}">@if($seat->show_title) {{ $seat->id }} @endif</a>
    </li> 
@endforeach

控制器示例是:

$seats = Seats::getSeats();
return view('whatever')->withSeats($seats);

問題的部分答案可能是讓showTitle函數之類的函數使用Laravel的whereIn函數,以便使SQL查詢可對ID數組進行操作,而不是按1乘1。這可以大大減少SQL查詢的數量。

這是我之前基本上使用的示例。
因此,在此示例中,您將使用一些循環來首先創建$arrayOfGroups ,然后將其用作wherein的參數

        $result = DB::table('users')
        ->whereIn('group',$arrayOfGroups)
        ->get();

我認為您可以根據自己的情況做類似的事情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM