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

ExpandRowColumn- expandRowKey or expandRowInd is not set to $model->id #884

Closed
9 tasks
romzzzzec opened this issue Mar 14, 2019 · 19 comments
Closed
9 tasks

Comments

@romzzzzec
Copy link

romzzzzec commented Mar 14, 2019

Prerequisites

  • [ x] I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • [ x] The issue still exists against the latest master branch of yii2-grid.
  • [x ] This is not an usage question. I confirm having gone through and read the documentation and demos.
  • [x ] This is not a general programming / coding question. (Those should be directed to the webtips Q & A forum).
  • [x ] I have attempted to find the simplest possible steps to reproduce the issue.
  • I have included a failing test as a pull request (Optional).

Steps to reproduce the issue

  1. $columns[] = [ 'class' => 'kartik\grid\ExpandRowColumn', 'width' => '50px', 'value' => function ($model, $key, $index, $column){ Yii::error(__METHOD__. ": debug expand - ". print_r($model, true)); return GridView::ROW_COLLAPSED; }, 'detailUrl' => Url::to(['authors-expand']), 'expandOneOnly' => true, 'disabled' => function ($model, $key, $index, $column){ return $model->authors ? false : true; }, ];

  2. log : `
    common\models\Author Object
    (
    [_id:common\models\Author:private] =>
    [_attributes:yii\db\BaseActiveRecord:private] => Array
    (
    [id] => 7157
    [id_user] => 30025
    [id_detail] => 1
    [created_at] => 2019-03-14 11:55:38
    [updated_at] => 2019-03-14 11:55:38
    )

    [_oldAttributes:yii\db\BaseActiveRecord:private] => Array
    (
    [id] => 7157
    [id_user] => 30025
    [id_detail] => 1
    [created_at] => 2019-03-14 11:55:38
    [updated_at] => 2019-03-14 11:55:38
    )
    ........
    `

Expected behavior and actual behavior

When I follow those steps, I see...
request body raw = 'expandRowKey=0&expandRowInd=0'

I was expecting...
expandRowInd=7157

Environment

Browsers

  • [ x ] Google Chrome
  • Mozilla Firefox
  • Internet Explorer
  • Safari

Operating System

  • Windows
  • Mac OS X
  • [ x ] Linux
  • Mobile

Libraries

  • jQuery version: 3.3.1
  • yii2-grid version: dev-master (16ca33e => 2145e8c): Checking out 2145e8cd1f

Isolating the problem

  • This bug happens on the demos page
  • The bug happens consistently across all tested browsers
  • [ x ] This bug happens when using yii2-grid without other plugins.
@adamwinn
Copy link

adamwinn commented Apr 2, 2019

I would like to see this implemented. We need more data given from the call.

@kartik-v
Copy link
Owner

kartik-v commented Apr 3, 2019

This works as intended. Cross check the grid demo where it is been updated to include a detailUrl for the ExpandRowColumn - check the browser console for the ajax call responses.

@kartik-v
Copy link
Owner

kartik-v commented Apr 3, 2019

On probably another related issue, in case you have updated jquery versions, I will push an update for jquery version >= 3.x where jQuery.load functionality does not work. It will be resolved separately.

@kartik-v
Copy link
Owner

kartik-v commented Apr 3, 2019

Have incorporated enhancement to replace jQuery,load with jQuery.ajax. Cannot reproduce your other issue. Cross check demos and confirm working fine. If you have a method to reproduce the error - please share and will reopen.

@kartik-v kartik-v closed this as completed Apr 3, 2019
@adamwinn
Copy link

adamwinn commented Apr 3, 2019

A rowID that is passed in the POST is worthless to me. I need something like the model's ID. I dont know what data is on row 5, for example. And plus, what happens after sorting and filtering?

I need to do something like 'detailUrl'=>Url::to(['/site/book-details', 'id' => $model->item_id]). That's exactly what the author of this bug wants too.

@kartik-v
Copy link
Owner

kartik-v commented Apr 3, 2019

That is the model primary key which is passed in expandRowKey... Not sure what your issue is. You can cross check the demo on how each row fetches the data specific to the model.

@adamwinn
Copy link

adamwinn commented Apr 3, 2019

I know how you fetch the data in the demo but my table and DB structure is completely different. Your demos a very simple and include only 1 table with minimal information.

The RowID given to me is 5, which corresponds to the row that I expanded. The PK for that row is not 5, but instead an internal ID such as 2837282. Why would a row in a table correspond to a PK in a table? I have grids that are very complex with many tables joined and many columns. RowID 5 tells me nothing about what row is expanded. I need to be able to access $model->item_id or $model->id, not RowID = 5. A lot of other people want a closure so we can access the model. #201 #883

@adamwinn
Copy link

adamwinn commented Apr 3, 2019

Read the issue from the original author of this bug:

When I follow those steps, I see...
request body raw = 'expandRowKey=0&expandRowInd=0'

I was expecting...
expandRowInd=7157

If the grid youre showing has primary keys in the 7,000s, POSTing expandRowKey=0&expandRowInd=0 is worthless and doesn't help you find the data in the DB. Doing $model = \app\models\Book::findOne($_POST['expandRowKey']); will obviously fail since expandRowKey is 0 and not 7157

@kartik-v
Copy link
Owner

kartik-v commented Apr 3, 2019

Again it is not the row id which you are stating but the primary key value (typically $model->id which is defined within the database for the row which must be returned as expandRowKey). It is the primary key value that is automatically parsed via ActiveRecord primaryKey method and actually the default yii2 feature that is used by this extension via the getDataCellValue methods within the default yii\grid\DataColumn . So my point was only to tell you that and debug your code if your dataprovider is based on ActiveDataProvider. The default yii2 grid sets these primary keys against each table row as data-key property which is also returned by the ExpandRowColumn.

So if you folks are getting an incorrect expandRowKey it means your ActiveDataProvider and ActiveRecord is not correctly set and is not parsing the correct primary key and you need to check that (e.g. you could be building your ActiveRecord on a database view - which will not auto fetch a primary key and you need to override those methods to return the correct primary key).

If you need to index by the primary key - then use the indexBy method in your query that generates the dataProvider. This will also give your expand row index to match the primary key value. It in the end depends on how you program and code your data source and configure that to achieve your outcome.

With regards to enhancing to accept Closure for detailUrl will need to look into another enhancement for the same. Note that when setting a detailUrl, the extension uses javascript and ajax based calls to render the data dynamically unlike the detail property which uses PHP code to render. Javascript will not be able to parse complex variables like the PHP Closure method - so it needs a bit of thought and not a straightforward/ trivial enhancement.

@adamwinn
Copy link

adamwinn commented Apr 3, 2019

I dont even have a $model->id defined for this grid.

@kartik-v
Copy link
Owner

kartik-v commented Apr 3, 2019

Yes and that is where the problem is as I was stating - if your data source has a primaryKey value defined it will be used by default and if you have a different non-standard data structure (not supported by default yii2 ActiveRecord) - then you would need to write your code appropriately. For example, you may need to override the primaryKey method to return your unique identifier as needed or use indexBy to index your query data in your dataProvider based on another column.

@adamwinn
Copy link

adamwinn commented Apr 3, 2019

I'm using ActiveDataProvider, but it is a union and complex

yii\data\ActiveDataProvider Object
(
    [query] => yii\db\Query Object
        (
            [select] => 
            [selectOption] => 
            [distinct] => 
            [from] => Array
                (
                    [dummy_name] => yii\db\ActiveQuery Object
                        (
                            [sql] => 
                            [on] => 
                            [joinWith] => Array
                                (
                                    [0] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [0] => item
                                                    [1] => eventItemStore
                                                )

                                            [1] => 1
                                            [2] => LEFT JOIN
                                        )

                                )

                            [select] => Array
                                (
                                    [0] => event_item.event_id
                                    [1] => event_item.event_item_id
                                    [2] => event_item.upc
                                    [3] => event_item.line_code
                                    [4] => item.item_description
                                    [5] => event_item.item_number
                                    [6] => event_item.department
                                    [7] => event_item.comment
                                    [8] => event_item.price
                                    [9] => event_item.price_multiple
                                    [10] => event_item.ad_location
                                    [11] => event_item.display
                                    [12] => event_item.display_upc
                                    [13] => event_item.display_type
                                    [14] => event_item.dc
                                    [15] => item.pack as pack
                                    [16] => item.size as size
                                    [17] => date_price_chosen
                                    [18] => historic_end_date
                                    [19] => store_lock
                                    [20] => show_waves
                                    [21] => deal_cost
                                    [22] => store_reviewed
                                )

                            [selectOption] => 
                            [distinct] => 
                            [from] => 
                            [groupBy] => Array
                                (
                                    [0] => event_item.upc
                                    [1] => event_item.item_number
                                )

                            [join] => 
                            [having] => 
                            [union] => Array
                                (
                                    [0] => Array
                                        (
                                            [query] => yii\db\ActiveQuery Object
                                                (
                                                    [sql] => 
                                                    [on] => 
                                                    [joinWith] => Array
                                                        (
                                                            [0] => Array
                                                                (
                                                                    [0] => Array
                                                                        (
                                                                            [0] => item
                                                                            [1] => eventItemStore
                                                                        )

                                                                    [1] => 1
                                                                    [2] => LEFT JOIN
                                                                )

                                                        )

                                                    [select] => Array
                                                        (
                                                            [0] => event_item.event_id
                                                            [1] => event_item.event_item_id
                                                            [2] => event_item.upc
                                                            [3] => event_item.line_code
                                                            [4] => item.item_description
                                                            [5] => event_item.item_number
                                                            [6] => event_item.department
                                                            [7] => event_item.comment
                                                            [8] => event_item.price
                                                            [9] => event_item.price_multiple
                                                            [10] => event_item.ad_location
                                                            [11] => event_item.display
                                                            [12] => event_item.display_upc
                                                            [13] => event_item.display_type
                                                            [14] => event_item.dc
                                                            [15] => item.pack as pack
                                                            [16] => item.size as size
                                                            [17] => date_price_chosen
                                                            [18] => historic_end_date
                                                            [19] => store_lock
                                                            [20] => show_waves
                                                            [21] => deal_cost
                                                            [22] => store_reviewed
                                                        )

                                                    [selectOption] => 
                                                    [distinct] => 
                                                    [from] => 
                                                    [groupBy] => Array
                                                        (
                                                            [0] => event_item.line_code
                                                        )

                                                    [join] => 
                                                    [having] => 
                                                    [union] => 
                                                    [params] => Array
                                                        (
                                                        )

                                                    [queryCacheDuration] => 
                                                    [queryCacheDependency] => 
                                                    [_events:yii\base\Component:private] => Array
                                                        (
                                                        )

                                                    [_eventWildcards:yii\base\Component:private] => Array
                                                        (
                                                        )

                                                    [_behaviors:yii\base\Component:private] => Array
                                                        (
                                                        )

                                                    [where] => Array
                                                        (
                                                            [0] => and
                                                            [1] => Array
                                                                (
                                                                    [event_item.event_id] => 39155
                                                                    [store_id] => Array
                                                                        (
                                                                            [111] => 111
                                                                        )

                                                                )

                                                            [2] => Array
                                                                (
                                                                    [0] => >
                                                                    [1] => line_code
                                                                    [2] => 0
                                                                )

                                                            [3] => Array
                                                                (
                                                                    [0] => or
                                                                    [1] => Array
                                                                        (
                                                                            [event_item.display_type] => Array
                                                                                (
                                                                                    [0] => 
                                                                                    [1] => D
                                                                                    [2] => d
                                                                                )

                                                                        )

                                                                    [2] => Array
                                                                        (
                                                                            [event_item.display_type] => 
                                                                        )

                                                                )

                                                        )

                                                    [limit] => 
                                                    [offset] => 
                                                    [orderBy] => 
                                                    [indexBy] => 
                                                    [emulateExecution] => 
                                                    [modelClass] => app\models\EventItem
                                                    [with] => 
                                                    [asArray] => 
                                                    [multiple] => 
                                                    [primaryModel] => 
                                                    [link] => 
                                                    [via] => 
                                                    [inverseOf] => 
                                                    [viaMap:yii\db\ActiveQuery:private] => 
                                                )

                                            [all] => 
                                        )

                                )

                            [params] => Array
                                (
                                )

                            [queryCacheDuration] => 
                            [queryCacheDependency] => 
                            [_events:yii\base\Component:private] => Array
                                (
                                )

                            [_eventWildcards:yii\base\Component:private] => Array
                                (
                                )

                            [_behaviors:yii\base\Component:private] => Array
                                (
                                )

                            [where] => Array
                                (
                                    [0] => and
                                    [1] => Array
                                        (
                                            [event_item.event_id] => 39155
                                            [store_id] => Array
                                                (
                                                    [111] => 111
                                                )

                                        )

                                    [2] => Array
                                        (
                                            [0] => or
                                            [1] => Array
                                                (
                                                    [line_code] => 0
                                                )

                                            [2] => Array
                                                (
                                                    [line_code] => 
                                                )

                                        )

                                    [3] => Array
                                        (
                                            [0] => or
                                            [1] => Array
                                                (
                                                    [event_item.display_type] => Array
                                                        (
                                                            [0] => 
                                                            [1] => D
                                                            [2] => d
                                                        )

                                                )

                                            [2] => Array
                                                (
                                                    [event_item.display_type] => 
                                                )

                                        )

                                )

                            [limit] => 
                            [offset] => 
                            [orderBy] => 
                            [indexBy] => 
                            [emulateExecution] => 
                            [modelClass] => app\models\EventItem
                            [with] => 
                            [asArray] => 
                            [multiple] => 
                            [primaryModel] => 
                            [link] => 
                            [via] => 
                            [inverseOf] => 
                            [viaMap:yii\db\ActiveQuery:private] => 
                        )

                )

            [groupBy] => 
            [join] => 
            [having] => 
            [union] => 
            [params] => Array
                (
                )

            [queryCacheDuration] => 
            [queryCacheDependency] => 
            [_events:yii\base\Component:private] => Array
                (
                )

            [_eventWildcards:yii\base\Component:private] => Array
                (
                )

            [_behaviors:yii\base\Component:private] => 
            [where] => 
            [limit] => 
            [offset] => 
            [orderBy] => 
            [indexBy] => 
            [emulateExecution] => 
        )

    [key] => 
    [db] => 
    [id] => 
    [_sort:yii\data\BaseDataProvider:private] => yii\data\Sort Object
        (
            [enableMultiSort] => 
            [attributes] => Array
                (
                    [upc] => Array
                        (
                            [asc] => Array
                                (
                                    [event_item.upc] => 4
                                )

                            [desc] => Array
                                (
                                    [event_item.upc] => 3
                                )

                            [label] => UPC
                        )

                    [item_number] => Array
                        (
                            [asc] => Array
                                (
                                    [event_item.item_number] => 4
                                )

                            [desc] => Array
                                (
                                    [event_item.item_number] => 3
                                )

                            [label] => CIC
                        )

                    [item_description] => Array
                        (
                            [asc] => Array
                                (
                                    [item_description] => 4
                                )

                            [desc] => Array
                                (
                                    [item_description] => 3
                                )

                            [label] => Item Description
                        )

                    [pack] => Array
                        (
                            [asc] => Array
                                (
                                    [pack] => 4
                                )

                            [desc] => Array
                                (
                                    [pack] => 3
                                )

                            [label] => Pack
                        )

                    [size] => Array
                        (
                            [asc] => Array
                                (
                                    [size] => 4
                                )

                            [desc] => Array
                                (
                                    [size] => 3
                                )

                            [label] => Size
                        )

                    [display_type] => Array
                        (
                            [asc] => Array
                                (
                                    [display_type] => 4
                                )

                            [desc] => Array
                                (
                                    [display_type] => 3
                                )

                            [label] => Display Type
                        )

                    [price] => Array
                        (
                            [asc] => Array
                                (
                                    [price] => 4
                                )

                            [desc] => Array
                                (
                                    [price] => 3
                                )

                            [label] => Price
                        )

                    [model_type] => Array
                        (
                            [asc] => Array
                                (
                                    [model_type] => 4
                                )

                            [desc] => Array
                                (
                                    [model_type] => 3
                                )

                            [label] => Model Type
                        )

                    [incr_decr] => Array
                        (
                            [asc] => Array
                                (
                                    [incr_decr] => 4
                                )

                            [desc] => Array
                                (
                                    [incr_decr] => 3
                                )

                            [label] => Incr Decr
                        )

                    [line_code] => Array
                        (
                            [asc] => Array
                                (
                                    [line_code] => 4
                                )

                            [desc] => Array
                                (
                                    [line_code] => 3
                                )

                            [label] => CIG
                        )

                    [department] => Array
                        (
                            [asc] => Array
                                (
                                    [department] => 4
                                )

                            [desc] => Array
                                (
                                    [department] => 3
                                )

                            [label] => Department
                        )

                    [ad_location] => Array
                        (
                            [asc] => Array
                                (
                                    [ad_location] => 4
                                )

                            [desc] => Array
                                (
                                    [ad_location] => 3
                                )

                            [label] => Ad Location
                        )

                    [display] => Array
                        (
                            [asc] => Array
                                (
                                    [display] => 4
                                )

                            [desc] => Array
                                (
                                    [display] => 3
                                )

                            [label] => Display
                        )

                    [category_manager_code] => Array
                        (
                            [asc] => Array
                                (
                                    [category_manager_code] => 4
                                )

                            [desc] => Array
                                (
                                    [category_manager_code] => 3
                                )

                            [label] => Manager Code
                        )

                    [comment] => Array
                        (
                            [asc] => Array
                                (
                                    [comment] => 4
                                )

                            [desc] => Array
                                (
                                    [comment] => 3
                                )

                            [label] => Comment
                        )

                    [date_price_chosen] => Array
                        (
                            [asc] => Array
                                (
                                    [date_price_chosen] => 4
                                )

                            [desc] => Array
                                (
                                    [date_price_chosen] => 3
                                )

                            [label] => Date Price Chosen
                        )

                    [store_lock] => Array
                        (
                            [asc] => Array
                                (
                                    [store_lock] => 4
                                )

                            [desc] => Array
                                (
                                    [store_lock] => 3
                                )

                            [label] => Store Lock
                        )

                    [show_waves] => Array
                        (
                            [asc] => Array
                                (
                                    [show_waves] => 4
                                )

                            [desc] => Array
                                (
                                    [show_waves] => 3
                                )

                            [label] => Show Waves
                        )

                    [deal_cost] => Array
                        (
                            [asc] => Array
                                (
                                    [deal_cost] => 4
                                )

                            [desc] => Array
                                (
                                    [deal_cost] => 3
                                )

                            [label] => Deal Cost
                        )

                )

            [sortParam] => sort
            [defaultOrder] => 
            [route] => 
            [separator] => ,
            [params] => 
            [urlManager] => 
            [_attributeOrders:yii\data\Sort:private] => 
        )

    [_pagination:yii\data\BaseDataProvider:private] => 
    [_keys:yii\data\BaseDataProvider:private] => 
    [_models:yii\data\BaseDataProvider:private] => 
    [_totalCount:yii\data\BaseDataProvider:private] => 
    [_events:yii\base\Component:private] => Array
        (
        )

    [_eventWildcards:yii\base\Component:private] => Array
        (
        )

    [_behaviors:yii\base\Component:private] => 
)

@adamwinn
Copy link

adamwinn commented Apr 3, 2019

$dataProvider->getKeys() returns

(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
)

lol

@romzzzzec
Copy link
Author

thanks, my code is based on ActiveDataProvider. indexBy solved problem. add this case to docs please

@adamwinn
Copy link

adamwinn commented Apr 3, 2019

I tried to override the primaryKey method but that didn't work so I used indexBy instead and it worked. Now I'm getting the correct PK POST'd. Thanks! :)

@kartik-v
Copy link
Owner

kartik-v commented Apr 3, 2019

Yes as I mentioned. Using indexBy in dataProvider is a configuration that will work for the issues you are having. These are yii 2 inherent functionality and suggest to go through the yii2 docs to achieve your use case.

@dmkdesign
Copy link

Kartik has mentioned a few time here and there that you can set the key used by the dataProvider to whichever attribute/column you want. This will be what is put in the expandRowKey during the action.

I do need the ability to create the url and pass other information though.

Example: My row of data is a summed value. The expanded row is the detailed rows that go into that summed result. I need access to the $model attributes to pass them into the ajax call so I can create a proper database query to get the specific rows that went into the result. I am able to access $searchModel variable so that is great, but the row has specific attributes I need to pass to the detailed view like the fact that the sum is of only "cash" payments.

image

//This is what I have done in order to get more specific information into the detailed view. It would be better to be able to load this data through a url though.
'detail'=>function($model) use ($searchModel)
{
$searchModel1 = clone $searchModel;
$searchModel1->created_by = $model['created_by'];
$searchModel1->transaction_type = $model['transaction_type'];
$dataProvider = $searchModel1->search(null);
$dataProvider->query->orderBy('created_by DESC');
$dataProvider->pagination=false;
$id = $model['created_by'];
return $this->render('payment_details',compact('dataProvider','searchModel1','id'));
}
//this is what I would like to do.
//'detailUrl' => Url::to(['payment-details','locationId' => $searchModel->locationId, 'dateRange'=>$searchModel->dateRange,'transactionType'=>$model['transaction_type']]),

@barw4
Copy link

barw4 commented Nov 20, 2019

I've been having same problem as well, however I think I've found a temporary solution.

$dataProvider = new ActiveDataProvider([ 'query' => $query, 'key' => function ($model) { return [$model->param1 => $model->param2]; }, 'sort' => ['defaultOrder' => ['param1' => SORT_DESC]], ]);

What you can do is set "key" value in ActiveDataProvider object as a callable array, then in "ExpandRowKey" you get an array. That solves some problems.

@dmkdesign
Copy link

I've been having same problem as well, however I think I've found a temporary solution.

$dataProvider = new ActiveDataProvider([ 'query' => $query, 'key' => function ($model) { return [$model->param1 => $model->param2]; }, 'sort' => ['defaultOrder' => ['param1' => SORT_DESC]], ]);

What you can do is set "key" value in ActiveDataProvider object as a callable array, then in "ExpandRowKey" you get an array. That solves some problems.

I don't know if this will serve my needs but this is SUPER powerful and one of the reasons I love Yii. Definitely kind of a hack but this allows some pretty cool functionality now. Will take a look. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants