added cache to get default country and currency

This commit is contained in:
Julien Chanséaume
2014-07-02 09:55:14 +02:00
parent ec71959934
commit 3a9ce68694
2 changed files with 19 additions and 9 deletions

View File

@@ -16,6 +16,8 @@ class Country extends BaseCountry
{ {
use \Thelia\Model\Tools\ModelEventDispatcherTrait; use \Thelia\Model\Tools\ModelEventDispatcherTrait;
protected static $defaultCountry = null;
/** /**
* *
* Put the current country as the default one. * Put the current country as the default one.
@@ -93,16 +95,19 @@ class Country extends BaseCountry
/** /**
* Return the default country * Return the default country
* *
* @throws LogicException if no default country is defined * @throws \LogicException if no default country is defined
*/ */
public static function getDefaultCountry() public static function getDefaultCountry()
{ {
$dc = CountryQuery::create()->findOneByByDefault(true); if (null === self::$defaultCountry) {
self::$defaultCountry = CountryQuery::create()->findOneByByDefault(true);
if ($dc == null) if (self::$defaultCountry == null) {
throw new \LogicException(Translator::getInstance()->trans("Cannot find a default country. Please define one.")); throw new \LogicException(Translator::getInstance()->trans("Cannot find a default country. Please define one."));
}
}
return $dc; return self::$defaultCountry;
} }
/** /**

View File

@@ -14,15 +14,20 @@ class Currency extends BaseCurrency
use \Thelia\Model\Tools\PositionManagementTrait; use \Thelia\Model\Tools\PositionManagementTrait;
protected static $defaultCurrency = null;
public static function getDefaultCurrency() public static function getDefaultCurrency()
{ {
$currency = CurrencyQuery::create()->findOneByByDefault(1); if (null === self::$defaultCurrency) {
if (null === $currency) { self::$defaultCurrency = CurrencyQuery::create()->findOneByByDefault(1);
throw new \RuntimeException("No default currency is defined. Please define one.");
if (self::$defaultCurrency == null) {
throw new \RuntimeException("No default currency is defined. Please define one.");
}
} }
return $currency; return self::$defaultCurrency;
} }
/** /**