我需要一个指示,没有能够找到任何关于它(但也许这是我的错.)。一个指示也到蛋糕书将是罚款。
在我的应用程序中,我有PatientsTable
。一个Patient
有许多个Notes
,每个Note
属于一个NoteType
。
比如说
class PatientsTable extends AppTable
{
public function initialize(array $config): void
{
// ...
$this->hasMany('Notes', [
'foreignKey' => 'patient_id',
]);
}
}
class NotesTable extends AppTable
{
public function initialize(array $config): void
{
// ...
$this->belongsTo('NotesTypes', [
'foreignKey' => 'notes_type_id',
'joinType' => 'INNER',
]);
$this->belongsTo('Patients', [
'foreignKey' => 'patient_id',
'joinType' => 'INNER',
]);
}
class NotesTypesTable extends AppTable
{
public function initialize(array $config): void
{
// ...
$this->hasMany('Notes', [
'foreignKey' => 'notes_type_id',
]);
}
}
现在,对于每个Patient
,我想检索每个NoteType
的最后一个(使用limit(1)
和orderDesc('date')
)Note
。
如果需要,我还可以执行单独的查询来检索患者ID。这不是问题然后,对每个患者运行一个新的查询(这将使查询总数为patients + 1,但这对缓存来说很好)。
现在我正试图检索单个患者的数据,只是想看看它是否有效。如:
$noteTypes = $this->Notes->NotesTypes->find()
->contain('Notes', function (Query $Query): Query {
return $Query
->matching('Patients', function (Query $Query): Query {
return $Query->where(['Patients.id' => 35]);
})
->orderDesc('date')
->limit(1);
})
->all();
但它肯定不起作用,以至于我这样得到的数据对我来说甚至没有意义。
你有什么建议或指示吗?
谢谢
1条答案
按热度按时间6kkfgxo01#
您试图实现的是一个常见问题,可以通过在患者表中保存**可空列“last_note_id”**来解决
当你在NotesTable add的“afterSave()”回调中保存一个注解时,
然后添加一个
通过始终使用LastNotes而不是Notes来检索最后一个条目