2

I have a "Pages" model with a Translate behavior. And I want to search a page matching in another language with CakePHP 3.x.

class PagesTable extends Table
{
    public function initialize(array $config)
    {

        $this->addBehavior('Translate', ['fields' => ['title', 'slug']]);
    }
 }

I have set the i18n locale in my controller before searching like so:

class PagesController extends AppController 
{

    /**
     * View method
     */
     public function view( $slug = '' )
     {
         I18n::locale('nl_NL');
         $this->Pages->findBySlug("foobar-in-nl")->first();
     }
 }

But sadly I won't get the record I want to. Is there a way to achieve this?

RedBeard
  • 79
  • 4
  • Possible duplicate of [How to query translated content when using the translate behavior?](http://stackoverflow.com/questions/31495745/how-to-query-translated-content-when-using-the-translate-behavior) – ndm May 16 '17 at 17:45

1 Answers1

0

I think findBySlug() function cannot be work with this situation, because in your pages table you don`t have that field.

You should do it like this way:

public function view( $slug = '' )
     {
         I18n::locale('nl_NL');
         $this->Pages->find()
               ->where( ['Pages_slug_translation.content' => $slug] )
               ->firstOrFail();
     }
Csaba
  • 11
  • 1
  • 1