【问题标题】:Get translation from another model in Entity in CakePhp从 CakePhp 中的实体中的另一个模型获取翻译
【发布时间】:2017-06-27 01:24:39
【问题描述】:

我正在按照这个示例https://book.cakephp.org/3.0/en/quickstart.html#creating-the-finder-method 来学习 CakePhp,同时尝试使其适应我的情况。此时,我有一个修剪模型,我想找到与某个文本匹配的修剪(如“%text%”)。

问题是:由于我需要为项目翻译,我有一个带有条目模型的翻译模型,该模型存储所有可显示的文本,如修剪名称。

关联:

修剪表:

$this->belongsTo('translation', [
        'foreignKey' => 'name',
        'bindingKey' => 'idTranslation'
    ]);

翻译表:

$this->hasOne('trim', [
        'foreignKey' => 'name',
        'bindingKey' => 'idTranslation'
    ]);

$this->hasMany('entry', [
        'foreignKey' => 'idTranslation',
        'bindingKey' => 'idTranslation',
        'dependent' => 'true'
    ]);

条目表:

$this->belongsTo('translation', [
        'foreignKey' => 'idTranslation',
        'bindingKey' => 'idTranslation'
    ]);

我设法执行了 findLike 函数并仅找到带有特定文本的修剪,但我找不到如何显示文本而不是名称字段中包含的 id。

TrimTable 中的 findLike 函数(只是硬编码搜索的示例):

public function findLike(Query $query, array $options) {
    $trims = $query->matching('Translation.Entry', function ($q) {
        return $q->where(['text LIKE' => 'LD3E']);
    });
    return $trims;
}

这是我的视图 name_search.ctp :

<h1>
 Trims named like
 <?= $this->Text->toList(h($noms)) ?>
</h1>

<section>
<?php foreach ($trims as $trim): ?>
<article>
    <!-- Use the HtmlHelper to create a link -->
    <h4><?= h($trim->name) ?></h4>
    <small><?= h($trim->idProduit) ?></small>

    <!-- Use the TextHelper to format text -->
    <?= $this->Text->autoParagraph(h($trim->description)) ?>
</article>

this is the result 447应该是LD3E。那么如何让我的 findLike 函数返回一个结果集,其中包含要显示的翻译?

【问题讨论】:

    标签: php cakephp cakephp-3.0


    【解决方案1】:

    可以在_matchingData 属性中找到匹配数据,它将包含一个实体数组,以匹配的关联名称为键。

    $trim->_matchingData['Entry']->text
    

    顺便说一句,。查找器应该返回查询对象,而不是结果集,即不要调用all()!大多数时候,您应该修改参数中传递的查询,而不是通过调用$this-&gt;find() 创建一个新查询。

    CakePHP 还附带开箱即用的翻译功能,您可能想看看它,以防万一您不知道。

    另见

    【讨论】:

    • 是的,我看到了翻译功能,但遗憾的是我使用的是旧数据库,无法更改。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    • 2017-07-06
    • 1970-01-01
    相关资源
    最近更新 更多