簡體   English   中英

Cakephp 3-如何使用查找查詢通過翻譯查找實體

[英]Cakephp 3 - How to find using the find query an entity by its translation

我有一個具有翻譯行為的“頁面”模型。 我想用CakePHP 3.x搜索另一種語言的匹配頁面。

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

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

在像這樣搜索之前,我已經在控制器中設置了i18n語言環境:

class PagesController extends AppController 
{

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

但是可悲的是我沒有我想要的唱片。 有沒有辦法做到這一點?

我認為findBySlug()函數無法在這種情況下使用,因為在您的頁面表中您沒有該字段。

您應該這樣做:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM