Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fixed can sorting cutted array with CArrayDataProvider #1534

Merged
merged 6 commits into from

5 participants

Evgeny Blinov Carsten Brandt Maurizio Domba Cerin Alexander Makarov Alexander Kochetov
Evgeny Blinov

Sample code

$testArray = array(
    array('id' => 1, 'field1' => 'value1', 'field2' => 'value2'),
    array('id' => 2, 'field1' => 'value3'),
);

$dataProvider = new CArrayDataProvider($testArray,array(
    'sort' => array(
        'attributes' => array('id', 'field1', 'field2')
    )
));

$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider' => $dataProvider,
    'columns' => array('field1', 'field2')
));

Initial rendered correctly, but sort this grid by field2 thrown exception Undefined index: field2 on framework/web/CArrayDataProvider.php(160)

framework/web/CArrayDataProvider.php
@@ -156,8 +156,12 @@ protected function sortData($directions)
*/
protected function getSortingFieldValue($data, $fields)
{
- foreach ($fields as $field)
- $data = is_object($data) ? $data->$field : $data[$field];
+ foreach($fields as $field){
+ if(is_object($data))
+ $data = isset($data->$field)?$data->$field:null;

$data=isset($data->$field) ? $data->$field : null;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
framework/web/CArrayDataProvider.php
@@ -156,8 +156,12 @@ protected function sortData($directions)
*/
protected function getSortingFieldValue($data, $fields)
{
- foreach ($fields as $field)
- $data = is_object($data) ? $data->$field : $data[$field];
+ foreach($fields as $field){
+ if(is_object($data))
+ $data = isset($data->$field)?$data->$field:null;
+ else
+ $data = isset($data[$field])?$data[$field]:null;

same here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Carsten Brandt
Collaborator

This is about #1531

CHANGELOG
@@ -3,6 +3,7 @@
Version 1.1.13 work in progress
-------------------------------
+- Bug: Fixed can sorting cutted array with CArrayDataProvider (Yiivgeny)
Carsten Brandt Collaborator
cebe added a note

add issue number #1531 and make sure, changelog lines are sorted by number.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
framework/web/CArrayDataProvider.php
@@ -156,8 +156,12 @@ protected function sortData($directions)
*/
protected function getSortingFieldValue($data, $fields)
{
- foreach ($fields as $field)
- $data = is_object($data) ? $data->$field : $data[$field];
+ foreach($fields as $field){
+ if(is_object($data))
+ $data=isset($data->$field) ? $data->$field : null;
+ else
+ $data=isset($data[$field]) ? $data[$field] : null;
+ }
Carsten Brandt Collaborator
cebe added a note

sry, missed that last time. Your indentation is wrong, please use tabs instead of spaces.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
CHANGELOG
@@ -25,6 +25,7 @@ Version 1.1.13 work in progress
- Bug #1444: Fixed CGoogleApi::register call to registerScriptFile (mdomba)
- Bug #1465: Fixed CHtml::beginForm() when CActiveForm with method GET and ajaxButton is used (mdomba)
- Bug #1485 CSort does not quote table alias when using CDbCriteria (undsoft)
+- Bug #1531: Fixed can sorting cutted array with CArrayDataProvider (Yiivgeny)
Carsten Brandt Collaborator
cebe added a note

I'd see this as an enhancement, also description can be improved a bit:

Enh #1531: CArrayDataProvider is now able to sort cutted array, where sorting column is not available in every entry (Yiivgeny)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Carsten Brandt cebe was assigned
Evgeny Blinov

@cebe, fixed.

Maurizio Domba Cerin
Collaborator

Regarding performace it would be better to put is_object() check outsite foreach{}

Alexander Makarov
Collaborator

Looks good to me. @mdomba merging?

Maurizio Domba Cerin
Collaborator

Yes, looks good to me too... I did not merge it because the issue is assigned to @cebe

Alexander Makarov samdark merged commit a793880 into from
Carsten Brandt
Collaborator

Yeah, assigned it to me, to merge it later, all fine :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 4, 2012
  1. Evgeny Blinov
Commits on Oct 5, 2012
  1. Evgeny Blinov

    CHANGELOG fix

    Yiivgeny authored
  2. Evgeny Blinov

    Coding style fix

    Yiivgeny authored
  3. Evgeny Blinov

    Coding style fix

    Yiivgeny authored
  4. Evgeny Blinov

    CHANGELOG fix

    Yiivgeny authored
  5. Evgeny Blinov

    Performance optimization

    Yiivgeny authored
This page is out of date. Refresh to see the latest.
Showing with 11 additions and 2 deletions.
  1. +1 −0  CHANGELOG
  2. +10 −2 framework/web/CArrayDataProvider.php
1  CHANGELOG
View
@@ -49,6 +49,7 @@ Version 1.1.13 work in progress
- Enh #1426: Behaviors are now affecting memory consumption significantly less (slavcodev, creocoder, Qiang, samdark)
- Enh #1443: Added CHttpRequest::getRawBody() that allows reading RAW HTTP request body multiple times (itamar82, resurtm, samdark)
- Enh #1518: Allow to configure CHtml::$closeSingleTags and CHtml::$renderSpecialAttributesValue. Useful for HTML5 code (creocoder)
+- Enh #1531: CArrayDataProvider is now able to sort cutted array, where sorting column is not available in every entry (Yiivgeny)
- Enh: Fixed the check for ajaxUpdate false value in jquery.yiilistview.js as that never happens (mdomba)
- Enh: Requirements checker: added check for Oracle database (pdo_oci extension) and MSSQL (pdo_dblib, pdo_sqlsrv and pdo_mssql extensions) (resurtm)
- Enh: Added CChainedLogFilter class to allow adding multiple filters to a logroute (cebe)
12 framework/web/CArrayDataProvider.php
View
@@ -156,8 +156,16 @@ protected function sortData($directions)
*/
protected function getSortingFieldValue($data, $fields)
{
- foreach ($fields as $field)
- $data = is_object($data) ? $data->$field : $data[$field];
+ if(is_object($data))
+ {
+ foreach($fields as $field)
+ $data=isset($data->$field) ? $data->$field : null;
+ }
+ else
+ {
+ foreach($fields as $field)
+ $data=isset($data[$field]) ? $data[$field] : null;
+ }
return $this->caseSensitiveSort ? $data : mb_strtolower($data,Yii::app()->charset);
}
Something went wrong with that request. Please try again.