Alright, the topic title may be a bit illusive, but I thought it would be beneficial to create a central topic for the more advanced problems and questions concerning xPDO and it’s functionality. I’ll start off with a problem of my own which I can’t seem to get solved. Shaun (splittingred) also replied to me on Twitter, but this solution didn’t work for me (while I am sure the solution itself is fine).
Perhaps some of these questions aren’t even possible, so they would make for a nice improvement on existing functions or new features.
My problem ánd question is with the getIterator function and getCount, in combination with grouping and distinct rows (unique). I want to use xPDO within MODx because it offers great flexibility in my queries, and so that I won’t have to type up huge chunks of SQL code. So, the problem! I want to SELECT DISTINCT rows from my database while maintaining the where clause. My current buildup is as follows:
$query = $this->modx->newQuery('history');
$query->where(array(
'userid' => 7
));
$query->groupby('location');
var_dump(count($this->modx->getCollection('history', $query)));
Now, what this does is select all rows from the model name "history" and group it by location. So what I am basically getting is 4 unique, out of 20 total results. This is perfect, however for such a simple query (in SQL), this portion of code is quite extensive. I wanted to dumb it down, like so:
var_dump($this->modx->getCount('history', array(
'userid' => 7,
'location:DISTINCT'
)));
This however, is simply not possible. The groupby on the other portion of code works as a DISTINCT, but still a bit more complicated than I want. See, I simply want a number of rows. Now, granted, this doesn’t have to work, but it should at least work with a query.
$query = $this->modx->newQuery('history');
$query->where(array(
'userid' => 7,
'location:DISTINCT'
));
// or
$query->where(array(
'userid' => 7
));
$query->distinct('location');
// or
$query->unique('location');
var_dump(count($this->modx->getCollection('history', $query)));
Now, this example doesn’t work because both distinct and unique do not exist within the xPDOObject class nor it’s extensions (as far as I know). I fiddled around with the Graph functions, but those seem to be more for join manipulations. Not really what I am trying to accomplish.
The second portion of code that acctually works, doesn’t work with getIterator, as it returns 2 as opposed to 4. When I loop over the $stack (object...) and push it into an array and thén count it, it works fine again.
So, what could be the best possible solution?