【发布时间】: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