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
Show file Open project: cakephp/orm Class Usage Examples

Protected Properties

Property 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

Public Methods

Method 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.

Protected Methods

Method 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 method

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 method

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
return void

_findExistingTranslations() protected method

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
return array

_referenceName() protected method

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.
return string

_rowMapper() protected method

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
return Cake\Collection\Collection

afterSave() public method

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
return void

beforeFind() public method

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
return void

beforeSave() public method

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
return void

buildMarshalMap() public method

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

findTranslations() public method

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
return Cake\ORM\Query

groupTranslations() public method

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.
return Cake\Collection\Collection

initialize() public method

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

locale() public method

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
return string

setupFieldAssociations() public method

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
return void

Property Details

$_defaultConfig protected property

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

$_locale protected property

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

$_table protected property

Table instance
protected Table,Cake\ORM $_table
return Cake\ORM\Table

$_translationTable protected property

Instance of Table responsible for translating
protected Table,Cake\ORM $_translationTable
return Cake\ORM\Table