我有三个表ServerScans
,QueuedJobs
,ReportMalware
ReportMalware
具有type
列,该列包含诸如mail
,abc
。
我正在将serververcans查询为
$scan = $this->ServerScans->get($id, [
'contain' => [
'QueuedJobs',
'QueuedJobs.ReportMalware',
],
]);
并在视图中以两组使用for loop为
将恶意软件报告分开<h2>Mail report</h2>
<?php foreach($scan->queued_jobs->report_malware as $m): ?>
<?php if ($m->type == 'mail'): ?>
<?= $m->comment ?>
<?php endif; ?>
<?php endforeach;?>
<h2>Abc report</h2>
<?php foreach($scan->queued_jobs->report_malware as $m): ?>
<?php if ($m->type == 'abc'): ?>
<?= $m->comment ?>
<?php endif; ?>
<?php endforeach;?>
这将需要更多时间进行执行。
我想要的是将其保存在
中$scan = $this->ServerScans->get($id, [
'contain' => [
'QueuedJobs',
'QueuedJobs.ReportMalware.mail',
'QueuedJobs.ReportMalware.abc',
],
]);
是否有一种方法可以实现此目标并根据某些过滤条件两次包含相同的型号?
是的,您可以这样做。请参阅此处的文档。
同一表可以多次使用,以定义不同类型的 协会。例如,考虑要分开的情况 批准的评论以及尚未调节的评论:
这是从正式文档中获取的示例:
class ArticlesTable extends Table
{
public function initialize(array $config)
{
$this->hasMany('Comments')
->setConditions(['approved' => true]);
$this->hasMany('UnapprovedComments', [
'className' => 'Comments'
])
->setConditions(['approved' => false])
->setProperty('unapproved_comments');
}
}