Translate behavior / Shadow Table associated to other table

I have this tables

tbl terms:

  • id
  • name

tbl terms_translations

  • id
  • locale
  • name

tbl languages

  • id
  • name
  • locale

I attached the translate behavior with

        $this->addBehavior('Translate', [
           'strategyClass' => \Cake\ORM\Behavior\Translate\ShadowTableStrategy::class,
       ]);

to the terms table.

If I call in terms controller

        $term = $this->Terms->get($id, [
            'contain' => ['TermsTranslations'],
        ]);

the result is what I expected.

But I like to associate the terms_translations to languages.
Therefore I created a file “Termstranslationstable”:

<?php
declare(strict_types=1);

namespace App\Model\Table;

use Cake\ORM\Query;
use Cake\ORM\Table;

class TermstranslationsTable extends Table
{
    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config): void
    {
        parent::initialize($config);

        $this->setTable('terms_translations');
        $this->setDisplayField('name');

        $this->belongsTo('Languages', [
            'foreignKey' => 'locale',
            //'joinType' => 'INNER',
        ]);

    }

}

When I now in Termscontroller call:

$term = $this->Terms->get($id, [
            'contain' => ['TermsTranslations'=>['Languages']],
        ]);

I get this result as entity:

'term' => object(App\Model\Entity\Term) id:0 {
'id' => (int) 1
 'name' => 'sdfgsdg'
 '_i18n' => [
(int) 0 => object(App\Model\Entity\Termstranslation) id:3 {
'id' => (int) 1
 'locale' => 'fr_BL'
 'name' => 'dfgdgf465z4' 
'language' => null
 '[new]' => false 
'[accessible]' => [ ]
 '[dirty]' => [ ]
 '[original]' => [ ] 
'[virtual]' => [ ]
 '[hasErrors]' => false 
'[errors]' => [ ]
 '[invalid]' => [ ]
 '[repository]' => 'TermsTranslations'
},

As you can see the “language” in the “_i18n” is NULL.

edit:
the generated sql is this:

SELECT 
  TermsTranslations.id AS TermsTranslations__id, 
  TermsTranslations.locale AS TermsTranslations__locale, 
  TermsTranslations.name AS TermsTranslations__name, 
  Languages.id AS Languages__id, 
  Languages.name AS Languages__name, 
  Languages.locale AS Languages__locale, 
  Languages.gui AS Languages__gui 
FROM 
  terms_translations TermsTranslations 
  INNER JOIN (
    SELECT 
      (Terms.id) 
    FROM 
      terms Terms 
      INNER JOIN concepts Concepts ON Concepts.id = Terms.concept_id 
      LEFT JOIN termgenders Termgenders ON Termgenders.id = Terms.termgender_id 
      LEFT JOIN languages Languages ON Languages.id = Terms.language_id 
      LEFT JOIN termwordclasses Termwordclasses ON Termwordclasses.id = Terms.termwordclass_id 
      LEFT JOIN termtypes Termtypes ON Termtypes.id = Terms.termtype_id 
      LEFT JOIN termusagestates Termusagestates ON Termusagestates.id = Terms.termusagestate_id 
      INNER JOIN users Creators ON Creators.id = Terms.creator 
      INNER JOIN users Modifiers ON Modifiers.id = Terms.modifier 
    WHERE 
      Terms.id = 1 
    GROUP BY 
      Terms.id
  ) Terms ON TermsTranslations.id = Terms.id 
  LEFT JOIN languages Languages ON Languages.id = TermsTranslations.locale

So, the question is, how can I change this line
LEFT JOIN languages Languages ON Languages.id = TermsTranslations.locale

to
LEFT JOIN languages Languages ON Languages.locale = TermsTranslations.locale

Can someone maybe help me to get the languages into the translations?

OK, it was a typical “RTFM”.
In File “Termstranslationstable” I changed

        $this->belongsTo('Languages', [
            'foreignKey' => 'locale',
            //'joinType' => 'INNER',
        ]);

to

        $this->belongsTo('Languages', [
            'foreignKey' => 'locale',
            'bindingKey'=>'locale',
            //'joinType' => 'INNER',
        ]);

Sorry for wasting your time:-(

1 Like