获取数据库信息的更好方法

时间:2015-06-02 09:11:12

标签: php mysql laravel laravel-4

好吧,所以我有这个座位系统,生成一个30x30的网格,对于每个网格,数据库中有一行,但是当我在页面上生成这个网格时,我目前每个元素发出5个SQL请求(并且有900个元素),我们都知道这不是最优的。

为了渲染它,我做了一些带有一些检查的while循环,并回显出这段代码:

<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负载。

2 个答案:

答案 0 :(得分:1)

您的型号应如下所示:

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);

答案 1 :(得分:0)

您的问题的部分答案可能是让您的showTitle函数之类的函数使用Laravel的whereIn函数,这样您就可以让SQL查询作用于ID数组,而不是1比1这可以大大减少SQL查询的数量。

这是我以前基本上使用过的一个例子 因此,在此示例中,您将首先使用某个循环创建$arrayOfGroups,然后将其用作wherein的参数

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

我认为你可以在你的案件中做类似的事情。