Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActiveFinder::count improvement #2075

Closed
soeron opened this issue Feb 3, 2013 · 1 comment
Closed

ActiveFinder::count improvement #2075

soeron opened this issue Feb 3, 2013 · 1 comment
Assignees
Milestone

Comments

@soeron
Copy link

soeron commented Feb 3, 2013

Curently it is unable to use CActiveDataProvider as this:

$criteria = new CDbCriteria(array(
            'group' => 'some_field',
            'with' => 'some_relation',
            'together' => true,
));
$dataProvider = new CActiveDataProvider('SomeModel', array('criteria' => $criteria));

The result of $dataProivider->getTotalItemCount() will be incorrect, becouse of the group conditions being removed in CJoinElement::count();
To fix this behaviour some changes must be done.
The CJoinQuery class must implement new method for example

/**
 * Creates DbCriteria based on query attributes
 *
 * @return CDbCriteria
 */
public function toDbCriteria()
{
    $attrributes = array(
        'join' => implode(' ', $this->joins),
        'select' => implode(', ', $this->selects),
        'order' => implode(', ', $this->orders),
        'condition' => '',
        'group' => implode(', ', $this->groups),
        'having' => '',
        'limit' => $this->limit,
        'offset' => $this->offset,
        'params' => $this->params,
        'distinct' => $this->distinct,
    );
    if (!empty($this->conditions)) {
        $attributes['condition'] = '(' . implode(') AND (', $this->conditions) . ')';
    }
    if (!empty($this->havings)) {
        $attributes['having'] = '(' . implode(') AND (', $this->havings) . ')';
    }
    return new CDbCriteria($attributes);
}

In CJoinElement::count() the lines:

$query->orders=$query->groups=$query->havings=array();
$query->limit=$query->offset=-1;
$command=$query->createCommand($this->_builder);

should change to:

$query->limit=$query->offset=-1;
$query->orders = array();
$command = empty($query->groups)
    ? $query->createCommand($this->_builder)
    : $this->_builder->createCountCommand($this->_table, $query->toDbCriteria());

The other option is to create CJoinQuery method which implements same algorithm like CDbCommandBuilder::createCountCommand.

@ghost ghost assigned cebe Feb 3, 2013
@samdark
Copy link
Member

samdark commented Mar 5, 2013

Should be fixed now with #2167

@samdark samdark closed this as completed Mar 5, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants