2013-05-01 20 views
0

我需要根據父母的約束獲得一行的id。我想用雄辯的方式做到這一點,並保持優雅。這個過程開始時需要注意的一些事項: 我有 - country_code(2位iso),lang_code(語言的2位數字縮寫) 我需要 - country_id,lang_id(主鍵) 所以我可以得到--market_id查詢)Laravel 4雄辯 - 根據父母的約束獲取自我的id

我能找回我需要用下面的數據,比較遺憾的是變量的命名(客戶端有奇怪的名字):

// Only receive desired inputs 
$input_get = Input::only('marketCode','langCode'); 

// Need the country based on the "marketCode" 
$countryId = Country::where('code',$input_get['marketCode'])->pluck('id'); 

// Get the lang_id from "langCode" 
$languageId = Language::where('lang_abbr',$input_get['langCode'])->pluck('lang_id'); 

// Get the market_id from country_id and lang_id 
$marketId = Market::where('country_id', $countryId) 
        ->where('lang_id',$languageId)->pluck('market_id'); 

// Get All Market Translations for this market 
$marketTranslation = MarketTranslation::where('market_id',$marketId)->lists('ml_val','ml_key'); 

我試過以下,但這隻急於根據約束加載國家和語言。只有當market_id已知時,Eager Loading似乎纔有用。

class Market extends Eloquent { 
    protected $primaryKey = 'market_id'; 

    public function country() { 
     return $this->belongsTo('Country'); 
    } 

    public function language(){ 
     return $this->belongsTo('Language','lang_id'); 
    } 
} 


$markets = Market::with(array(
    'country' => function($query){ 
     $query->where('code','EE'); 
    }, 
    'language'=> function($query){ 
     $query->where('lang_abbr','et'); 
    } 
))->get(); 

回答

1

你必須使用連接才能做到這一點。

$market = Market::join('countries', 'countries.id', '=', 'markets.country_id') 
    ->join('languages', 'languages.id', '=', 'markets.language_id') 
    ->where('countries.code', '=', 'EE') 
    ->where('languages.lang_abbr', 'et') 
    ->first(); 

echo $market->id; 

如果這是一件經常發生,那麼我一個靜態方法可能增加市場模式。

// in class Market 
public static function lookup_id($country_code, $language_abbreviation) { ... } 

// then later 
$market_id = Market::lookup_id('EE', 'et'); 
+0

謝謝您的答覆。我探討了這個選項,但真的不想做手動連接或查詢。我真的想要依靠關係和ORM。我發佈了我想出的解決方案。 – 2013-05-01 23:57:34

0

看關係後,所以,我能得到它的工作,而無需使用人工的連接或查詢,只是在ORM定義的關係。這看起來是正確的,因爲它使用急切加載並過濾收集中所需的數據。

// Get A country object that contains a collection of all markets that use this country code 
    $country = Country::getCountryByCountryCode('EE'); 

    // Filter out the market in the collection that uses the language specified by langCode 
    $market = $country->markets->filter(function($market) { 
     if ($market->language->lang_abbr == 'et') { 
      return $market; 
     } 
    }); 

    // Get the market_id from the market object 
    $marketId = $market->first()->market_id; 

凡模型和關係是這樣的:

class Country extends Eloquent { 

    public function markets() { 
     return $this->hasMany('Market')->with('language'); 
    } 

    public static function getCountryByCountryCode($countryCode) 
    { 
     return Country::with('markets')->where('code',$countryCode)->first(); 
    } 
} 

class Market extends Eloquent { 
    protected $primaryKey = 'market_id'; 

    public function country() { 
     return $this->belongsTo('Country'); 
    } 

    public function language(){ 
     return $this->belongsTo('Language','lang_id'); 
    } 

} 



class Language extends Eloquent { 

    protected $primaryKey = 'lang_id'; 

}