PHP Class Cake\ORM\Behavior\TranslateBehavior

If you wish to override fields, you need to call the locale method in this behavior for setting the language you want to fetch from the translations table. If you want to bring all or certain languages for each of the fetched records, you can use the custom translations finders that is exposed to the table.
Inheritance: extends Cake\ORM\Behavior
Afficher le fichier Open project: cakephp/orm Class Usage Examples

Protected Properties

Свойство Type Description
$_defaultConfig array These are merged with user-provided configuration when the behavior is used.
$_locale string The locale name that will be used to override fields in the bound table from the translations table
$_table Cake\ORM\Table Table instance
$_translationTable Cake\ORM\Table Instance of Table responsible for translating

Méthodes publiques

Méthode Description
__construct ( Table $table, array $config = [] ) Constructor
afterSave ( Cake\Event\Event $event, Cake\Datasource\EntityInterface $entity ) : void Unsets the temporary _i18n property after the entity has been saved
beforeFind ( Cake\Event\Event $event, Query $query, ArrayObject $options ) : void Callback method that listens to the beforeFind event in the bound table. It modifies the passed query by eager loading the translated fields and adding a formatter to copy the values into the main table records.
beforeSave ( Cake\Event\Event $event, Cake\Datasource\EntityInterface $entity, ArrayObject $options ) : void Modifies the entity before it is saved so that translated fields are persisted in the database too.
buildMarshalMap ( $marshaller, $map, $options ) Add in _translations marshalling handlers if translation marshalling is enabled. You need to specifically enable translation marshalling by adding 'translations' => true to the options provided to Table::newEntity() or Table::patchEntity().
findTranslations ( Query $query, array $options ) : Query Custom finder method used to retrieve all translations for the found records.
groupTranslations ( Cake\Datasource\ResultSetInterface $results ) : Cake\Collection\Collection Modifies the results from a table find in order to merge full translation records into each entity under the _translations key
initialize ( array $config ) : void Initialize hook
locale ( string | null $locale = null ) : string Sets all future finds for the bound table to also fetch translated fields for the passed locale. If no value is passed, it returns the currently configured locale
setupFieldAssociations ( array $fields, string $table, string $model, string $strategy ) : void Creates the associations between the bound table and every field passed to this method.

Méthodes protégées

Méthode Description
_bundleTranslatedFields ( Cake\Datasource\EntityInterface $entity ) : void Helper method used to generated multiple translated field entities out of the data found in the _translations property in the passed entity. The result will be put into its _i18n property
_findExistingTranslations ( array $ruleSet ) : array Returns the ids found for each of the condition arrays passed for the translations table. Each records is indexed by the corresponding position to the conditions array
_referenceName ( Table $table ) : string Determine the reference name to use for a given table
_rowMapper ( Cake\Datasource\ResultSetInterface $results, string $locale ) : Cake\Collection\Collection Modifies the results from a table find in order to merge the translated fields into each entity for a given locale.

Method Details

__construct() public méthode

Constructor
public __construct ( Table $table, array $config = [] )
$table Cake\ORM\Table The table this behavior is attached to.
$config array The config for this behavior.

_bundleTranslatedFields() protected méthode

Helper method used to generated multiple translated field entities out of the data found in the _translations property in the passed entity. The result will be put into its _i18n property
protected _bundleTranslatedFields ( Cake\Datasource\EntityInterface $entity ) : void
$entity Cake\Datasource\EntityInterface Entity
Résultat void

_findExistingTranslations() protected méthode

Returns the ids found for each of the condition arrays passed for the translations table. Each records is indexed by the corresponding position to the conditions array
protected _findExistingTranslations ( array $ruleSet ) : array
$ruleSet array an array of arary of conditions to be used for finding each
Résultat array

_referenceName() protected méthode

The reference name is usually derived from the class name of the table object (PostsTable -> Posts), however for autotable instances it is derived from the database table the object points at - or as a last resort, the alias of the autotable instance.
protected _referenceName ( Table $table ) : string
$table Cake\ORM\Table The table class to get a reference name for.
Résultat string

_rowMapper() protected méthode

Modifies the results from a table find in order to merge the translated fields into each entity for a given locale.
protected _rowMapper ( Cake\Datasource\ResultSetInterface $results, string $locale ) : Cake\Collection\Collection
$results Cake\Datasource\ResultSetInterface Results to map.
$locale string Locale string
Résultat Cake\Collection\Collection

afterSave() public méthode

Unsets the temporary _i18n property after the entity has been saved
public afterSave ( Cake\Event\Event $event, Cake\Datasource\EntityInterface $entity ) : void
$event Cake\Event\Event The beforeSave event that was fired
$entity Cake\Datasource\EntityInterface The entity that is going to be saved
Résultat void

beforeFind() public méthode

Callback method that listens to the beforeFind event in the bound table. It modifies the passed query by eager loading the translated fields and adding a formatter to copy the values into the main table records.
public beforeFind ( Cake\Event\Event $event, Query $query, ArrayObject $options ) : void
$event Cake\Event\Event The beforeFind event that was fired.
$query Cake\ORM\Query Query
$options ArrayObject The options for the query
Résultat void

beforeSave() public méthode

Modifies the entity before it is saved so that translated fields are persisted in the database too.
public beforeSave ( Cake\Event\Event $event, Cake\Datasource\EntityInterface $entity, ArrayObject $options ) : void
$event Cake\Event\Event The beforeSave event that was fired
$entity Cake\Datasource\EntityInterface The entity that is going to be saved
$options ArrayObject the options passed to the save method
Résultat void

buildMarshalMap() public méthode

{@inheritDoc}
public buildMarshalMap ( $marshaller, $map, $options )

findTranslations() public méthode

Fetched translations can be filtered by locale by passing the locales key in the options array. Translated values will be found for each entity under the property _translations, containing an array indexed by locale name. ### Example: $article = $articles->find('translations', ['locales' => ['eng', 'deu'])->first(); $englishTranslatedFields = $article->get('_translations')['eng']; If the locales array is not passed, it will bring all translations found for each record.
public findTranslations ( Query $query, array $options ) : Query
$query Cake\ORM\Query The original query to modify
$options array Options
Résultat Cake\ORM\Query

groupTranslations() public méthode

Modifies the results from a table find in order to merge full translation records into each entity under the _translations key
public groupTranslations ( Cake\Datasource\ResultSetInterface $results ) : Cake\Collection\Collection
$results Cake\Datasource\ResultSetInterface Results to modify.
Résultat Cake\Collection\Collection

initialize() public méthode

Initialize hook
public initialize ( array $config ) : void
$config array The config for this behavior.
Résultat void

locale() public méthode

Sets all future finds for the bound table to also fetch translated fields for the passed locale. If no value is passed, it returns the currently configured locale
public locale ( string | null $locale = null ) : string
$locale string | null The locale to use for fetching translated records
Résultat string

setupFieldAssociations() public méthode

Additionally it creates a i18n HasMany association that will be used for fetching all translations for each record in the bound table
public setupFieldAssociations ( array $fields, string $table, string $model, string $strategy ) : void
$fields array list of fields to create associations for
$table string the table name to use for storing each field translation
$model string the model field value
$strategy string the strategy used in the _i18n association
Résultat void

Property Details

$_defaultConfig protected_oe property

These are merged with user-provided configuration when the behavior is used.
protected array $_defaultConfig
Résultat array

$_locale protected_oe property

The locale name that will be used to override fields in the bound table from the translations table
protected string $_locale
Résultat string

$_table protected_oe property

Table instance
protected Table,Cake\ORM $_table
Résultat Cake\ORM\Table

$_translationTable protected_oe property

Instance of Table responsible for translating
protected Table,Cake\ORM $_translationTable
Résultat Cake\ORM\Table