Yii2: Wrong record count in DataProvider when using JoinWith in Search

Created on 26 Oct 2017  Â·  6Comments  Â·  Source: yiisoft/yii2

What steps will reproduce the problem?

In the ModelSearch class, include a JoinWith parameter in the initial find statement. For example:

$query = Quote::find() ->joinWith(['author','source','themes']);

What is the expected result?

The number of records displayed at the beginning of GridView (or when grabbing $dataProvider->getTotalCount()) should be accurate.

If I remove the JoinWith statement, the GridView summary displays:

Showing 1-20 of 4,400 items.

What do you get instead?

In my case, it calculates there are almost 2000 more records than actually exist. Plus it changed the number per page:

Showing 1-15 of 6,242 items.

Please note that all that changed was that I removed the JoinWith statement.

Additional info

| Q | A
| ---------------- | ---
| Yii version | 2.0.12
| PHP version | 5.6.31
| Operating system | Linux 2.6.32-696.3.1.el6.x86_64 #1 SMP Tue May 30 19:52:55 UTC 2017

Most helpful comment

looks like your join produces duplicate rows, you can use DISTINCT to make them unique or adjust the join condition.

@OceanWind try it

$query = Quote::find() ->joinWith(['author','source','themes'])->distinct();

All 6 comments

looks like your join produces duplicate rows, you can use DISTINCT to make them unique or adjust the join condition. Note that if you only want eager loading only you do not need joinWith() but with().

U have a "have many" relation so u have duplicate rows. It is not a gridview error. But your query

looks like your join produces duplicate rows, you can use DISTINCT to make them unique or adjust the join condition.

@OceanWind try it

$query = Quote::find() ->joinWith(['author','source','themes'])->distinct();

Thank you! That was it.

Bill

Bill Sutton
[email protected]
720-722-4115

On Oct 26, 2017, at 1:48 AM, bscheshirwork [Masked] wrote:

Preview: > looks like your join produces duplicate rows, you can use D
This email is forwarded from a MASKED EMAIL you created using Blur https://dnt.abine.com/#help/faq/faq-whataremaskedemails.
IF THIS IS SPAM, CLICK HERE TO BLOCK. <https://dnt.abine.com/#/block_email/83682497@opayq.com/

Want to shop safely and privately online? Get Blur Premium https://dnt.abine.com/#premium.

looks like your join produces duplicate rows, you can use DISTINCT to make them unique or adjust the join condition.

@OceanWind https://github.com/oceanwind try it

$query = Quote::find() ->joinWith(['author','source','themes'])->distinct();
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub https://github.com/yiisoft/yii2/issues/15025#issuecomment-339581279, or mute the thread https://github.com/notifications/unsubscribe-auth/AHTx-IG-jsrKkq4VOXOYaIQuamGv52nQks5swDlggaJpZM4QGvUk.

thanks for solution

Thanks a lot for the solution

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AstRonin picture AstRonin  Â·  49Comments

samdark picture samdark  Â·  63Comments

deecode picture deecode  Â·  50Comments

sapsxxxil picture sapsxxxil  Â·  50Comments

alexandernst picture alexandernst  Â·  163Comments