[英]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.