This question is more about understanding xPDO than asking how it could work, because it does already...
The scenario is the following: I have a table with records of which each has a timestamp and a type field, the type can be assigned many times to different records. What I want is to fetch the newest/most recent record for each type (with a few types excluded, e.g. NOT IN). Additionally I need to fetch some information about the type from a separate types table (LEFT JOIN)... so like this I get what I want:
$query = new xPDOCriteria($modx, "SELECT * FROM (SELECT * FROM `table_with_records` WHERE type NOT IN('X','Y','Z') ORDER BY timestamp DESC) AS t LEFT JOIN `table_with_types` ON (t.typeid = table_with_types.id) GROUP BY t.type");
$objs = $modx->getCollection('ClassName', $query);
foreach ($objs as $obj) {
echo print_r($obj->toArray(),1);
}
but how would I write something like that as a "normal" xPDO query, especially the
FROM (SELECT...) AS t part...?
Something like:
$subquery = $modx->newQuery('ClassName');
$subquery->where('type:NOT IN' => array('X', 'Y', 'Z'));
$subquery->sortby('timestamp', 'DESC');
$query = $modx->newQuery('ClassName');
$query->leftJoin('JoinedClassName', 'Alias');
$query->groupby('type');
// how do I add $subquery to $query...??
[ed. note: exside last edited this post 9 years, 2 months ago.]