【问题标题】:Cakephp 3 - How to find using the find query an entity by its translationCakephp 3 - 如何使用 find 查询通过翻译来查找实体
【发布时间】:2023-04-03 00:34:01
【问题描述】:

我有一个带有翻译行为的“页面”模型。我想用 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();
     }
 }

但很遗憾,我不会得到我想要的唱片。有没有办法做到这一点?

【问题讨论】:

标签: php cakephp-3.x


【解决方案1】:

我认为 findBySlug() 函数不适用于这种情况,因为在您的 pages 表中您没有该字段。

你应该这样做:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多