RainLab\Translate\Models\Locale::findByCode PHP Method

findByCode() public static method

Locate a locale table by its code, cached.
public static findByCode ( string $code = null ) : Model
$code string
return Model
    public static function findByCode($code = null)
    {
        if (!$code) {
            return null;
        }
        if (isset(self::$cacheByCode[$code])) {
            return self::$cacheByCode[$code];
        }
        return self::$cacheByCode[$code] = self::whereCode($code)->first();
    }

Usage Example

示例#1
0
 public function prepareTable()
 {
     $fromCode = post('locale_from', null);
     $toCode = post('locale_to', Locale::getDefault()->code);
     $this->hideTranslated = post('hide_translated', false);
     /*
      * Page vars
      */
     $this->vars['hideTranslated'] = $this->hideTranslated;
     $this->vars['defaultLocale'] = Locale::getDefault();
     $this->vars['locales'] = Locale::all();
     $this->vars['selectedFrom'] = $selectedFrom = Locale::findByCode($fromCode);
     $this->vars['selectedTo'] = $selectedTo = Locale::findByCode($toCode);
     /*
      * Make table config, make default column read only
      */
     $config = $this->makeConfig('config_table.yaml');
     if (!$selectedFrom) {
         $config->columns['from']['readOnly'] = true;
     }
     if (!$selectedTo) {
         $config->columns['to']['readOnly'] = true;
     }
     /*
      * Make table widget
      */
     $widget = $this->makeWidget('Backend\\Widgets\\Table', $config);
     $widget->bindToController();
     /*
      * Populate data
      */
     $dataSource = $widget->getDataSource();
     $dataSource->bindEvent('data.getRecords', function ($offset, $count) use($selectedFrom, $selectedTo) {
         $messages = $count ? Message::orderBy('message_data', 'asc')->limit($count)->offset($offset)->get() : Message::orderBy('message_data', 'asc')->get();
         $result = $this->processTableData($messages, $selectedFrom, $selectedTo);
         return $result;
     });
     $dataSource->bindEvent('data.getCount', function () {
         return Message::count();
     });
     $dataSource->bindEvent('data.updateRecord', function ($key, $data) {
         $message = Message::find($key);
         $this->updateTableData($message, $data);
         CacheHelper::clear();
     });
     $dataSource->bindEvent('data.deleteRecord', function ($key) {
         if ($message = Message::find($key)) {
             $message->delete();
         }
     });
     $this->vars['table'] = $widget;
 }
All Usage Examples Of RainLab\Translate\Models\Locale::findByCode