I'm trying to understand some basic functionality and I'm not sure exactly what topic it falls under. I'm working in CakePHP and I'm trying to understand how these nested callback functions just seem to know what they're supposed to do. I think a lot of my problem is that I don't know what keywords to search so my question is this: how do the anonymous functions in CakePHP associations know a) that the parameter $q is a Query object and b) which model it's supposed to query from?
I can speculate that there's a) somewhere in CakePHP where anonymous functions are told to use the first parameter $q as a query and b) some handler that's traversing the object and assuming that any callback function it finds, no matter how deeply they are nested, is querying the model it's most directly nested under but I can't seem to find any documentation (or what keywords to search for) to understand how and why any of this should work.
Example 1 - this one knows $q is a query and that it's for the AuthorsTable
$query = $articles->find()->contain([
'Authors' => [
'foreignKey' => false,
'queryBuilder' => function ($q) {
return $q->where(...); // Full conditions for filtering
}
]
]);
Example 2 - This one knows $q is querying the ArticlesTable
$query = $authors->find();
$query->matching('Articles', function ($q) {
return $q->where(['Articles.created >=' => new DateTime('-10 days')]);
});
Thank you in advance... And I apologize if this is a question whose answer can be found on S.O. somewhere else; I'm sure the reason I couldn't find it is because I didn't know what special words to use.