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

ArraySource 2.0 branch doesn't support arrays in 'order' query option #73

Open
ghost opened this issue Oct 11, 2013 · 2 comments
Open
Labels

Comments

@ghost
Copy link

ghost commented Oct 11, 2013

Created by David Lancea, 19th Oct 2011. (originally Lighthouse ticket #29):


The ArraySource Datasource in the 2.0 branch on GitHub doesn't support arrays in 'order' query option.

Sample code:

// Works
var_dump( $this->ArrayModel->find('all', array('order'=>'field_name')) );

// Doesn't work
var_dump( $this->ArrayModel->find('all', array('order'=>array('field_name'=>'DESC'))) );

I believe the code responsible is in ArraySource.php, line 138, which checks if $queryData['order'][0] is a string, but does not bother to do anything if it's not.

@ghost
Copy link
Author

ghost commented Oct 11, 2013

19th Oct 2011, David Lancea said:


I fixed the problem using the following code:

Starting with line 136:

// Order
if (!empty($queryData['order']) && is_array($queryData['order'])) {
    foreach($queryData['order'] as $order){
        $alias = $model->alias;
        if (is_string($queryData['order'][0])) {
            $field = $order;
            if (strpos($field, '.') !== false) {
                list($alias, $field) = explode('.', $field, 2);
            }
            if (strpos($field, ' ') !== false) {
                list($field, $sort) = explode(' ', $field, 2);
            }
            if ($alias === $model->alias) {
                $sort = 'ASC';
                $data = Set::sort($data, '{n}.' . $model->alias . '.' . $field, $sort);
            }
        }else{
            foreach($order as $field => $sort) {
                if (strpos($field, '.') !== false) {
                    list($alias, $field) = explode('.', $field, 2);
                }
                if ($alias === $model->alias) {
                    if (is_numeric($field) || is_null($sort)) {
                        unset ($queryData['order'][$field]);
                        continue;
                    }
                    $data = Set::sort($data, '{n}.' . $model->alias . '.' . $field, $sort);
                }
            }
        }
    }
}

Also, line 476:

if (!empty($queryData['order'][0])) {
    foreach( $queryData['order'] as $order ){
        $orders[] = ( is_array($order) ? implode(', ', $order) : $order );
    } 

    $out .= ' ORDER BY ' . implode(', ', $orders );
}

@ghost
Copy link
Author

ghost commented Oct 11, 2013

19th Oct 2011, David Lancea said:


Update for line 476:

if (!empty($queryData['order'])) {
    $orders = array();
    foreach( $queryData['order'] as $order ){
        $orders[] = ( is_array($order) ? implode(' ', array_keys($order) + $order) : $order );
    }

    $out .= ' ORDER BY ' . implode(', ', $orders );
}

@AD7six AD7six added the array label Feb 17, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant