Exemplo n.º 1
0
 /**
  * Modifies the results from a table find in order to merge full translation records
  * into each entity under the `_translations` key
  *
  * @param \Cake\Datasource\ResultSetDecorator $results
  * @return \Cake\Collection\Collection
  */
 public function groupTranslations($results)
 {
     return $results->map(function ($row) {
         $translations = (array) $row->get('_i18n');
         $grouped = new Collection($translations);
         $result = [];
         foreach ($grouped->combine('field', 'content', 'locale') as $locale => $keys) {
             $translation = new Entity($keys + ['locale' => $locale], ['markNew' => false, 'useSetters' => false, 'markClean' => true]);
             $result[$locale] = $translation;
         }
         $options = ['setter' => false, 'guard' => false];
         $row->set('_translations', $result, $options);
         unset($row['_i18n']);
         $row->clean();
         return $row;
     });
 }
Exemplo n.º 2
0
 /**
  * Modifies the results from a table find in order to merge the translated fields
  * into each entity for a given locale.
  *
  * @param \Cake\DataSource\ResultSetDecorator $results
  * @param string $locale
  * @return \Cake\Collection\Collection
  */
 protected function _rowMapper($results, $locale)
 {
     return $results->map(function ($row) use($locale) {
         $options = ['setter' => false, 'guard' => false];
         foreach ($this->_config['fields'] as $field) {
             $name = $field . '_translation';
             $translation = $row->get($name);
             if (!$translation) {
                 continue;
             }
             $content = $translation->get('content');
             if ($content !== null) {
                 $row->set($field, $content, $options);
             }
             unset($row[$name]);
         }
         $row->set('_locale', $locale, $options);
         $row->clean();
         return $row;
     });
 }