데이터베이스에 3 개 MySQL의 테이블이 있습니다최적화 Laravel의 웅변 모델
제품 :
id | name
제품 가격 :
product_id | currency_id | price
통화 :
id | mark
그리고 Laravel의 웅변은 모델은 내가보기 이케이 :
// Product.php
class Product extends Eloquent {
protected $table = 'products';
protected $primaryKey = 'id';
public function prices(){
return $this->hasMany('ProductPrice', 'product_id', 'id');
}
}
// ProductPrice.php
class ProductPrice extends Eloquent {
protected $table = 'product_prices';
public function currency(){
return $this->hasOne('Currency', 'id', 'currency_id');
}
}
// Currency.php
class Currency extends Eloquent {
protected $table = 'currencies';
protected $primaryKey = 'id';
}
지금은 모든 가격으로 모든 제품을 표시해야합니다! 그리고 내 코드는 다음과 같습니다 : 코드는 잘 작동
$products = Product::with('prices')->get();
foreach($products as $product){
echo $product->name .'<br/>';
foreach($product->prices as $price){
echo $price->price .' '. $price->currency->mark .'<br/>';
}
echo '<hr/>';
}
, 그러나 너무 많은 SQL 쿼리 (많은 통화가 테이블에 저장됩니다으로 모든 제품이 많은 쿼리를 수행)이있다. 그렇다면 쿼리 작성기를 사용하지 않고 이러한 모델을 최적화하는 방법이 있습니까?
감사합니다.
http://stackoverflow.com/questions/23131565/laravel-4-select-related-models – Peter