Laravel 4雄辩 - 根据父母的限制获取自我的身份

时间:2013-05-01 15:56:16

标签: php laravel eager-loading laravel-4 eloquent

我需要根据父母的约束得到一行的id。我想用雄辩的方式做到这一点并保持优雅。此过程开始时需要注意的一些事项: 我有 - country_code(2位iso),lang_code(语言的2位数字缩写) 我需要 - country_id,lang_id(主键) 所以我可以得到 - market_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();

2 个答案:

答案 0 :(得分: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' );

答案 1 :(得分: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';

}