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?
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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.