I’m using Friends of Cake Search in my project, and I have a specific situation that requires an IN subquery in a WHERE clause. I thought the best way to handle this was with a callback in my search collection. The search field is configured like this:
<?php echo $this->Form->control('related_doc', [
'label' => 'Related Report',
'options' => $related_doc,
'empty' => [
'' => ''
]
]) ?>
The value of related_doc
comes from a dropdown in my search engine. and it’s the ID of a record in a table called ml_relations
. All I need to do is get the chosen value of related_doc
into my callback (see ‘WHAT GOES HERE??’ below):
$this->add('related_doc', 'Search.Callback', [
'callback' => function (\Cake\ORM\Query $query, array $args, \Search\Model\Filter\Base $filter) {
// Return all mission lessons with the selected related report)
$query->where("MissionLessons.id IN(SELECT ml_relations.ml_id
FROM ml_relations
WHERE ml_relations.related_noncon_id = WHAT GOES HERE??
AND ml_relations.deleted_record = 0)");
}
]);
I tested this out by hardcoding an ID in the IN subquery (e.g., WHERE ml_relations.related_noncon_id = 4262
), and I got back the results I expected. I’m just blanking on how to make that a variable. How do I reference the actual search field related_doc
in this callback?