Possible bug (?) on ActiveRecord

Hi, I have the following sentence:





$usmarc = UsmarcSubfield::find()

                        ->where(["tag" => 100, "subfield_cd" => "a"])

                        ->orWhere(["tag" => 650, "subfield_cd" => "a"])

                        ->orWhere(["tag" => 250, "subfield_cd" => "a"])

                        ->orWhere(["tag" => 10, "subfield_cd" => 'a'])

                        ->orWhere(["tag" => 20, "subfield_cd" => "a"])

                        ->orWhere(["tag" => 50, "subfield_cd" => ['a', 'b', 'c']])

                        ->orWhere(["tag" => 82, "subfield_cd" => ['a', '2']])

                        ->orWhere(["tag" => 260, "subfield_cd" => ['a', 'b', 'c']])

                        ->orWhere(["tag" => 520, "subfield_cd" => 'a'])

                        ->orWhere(["tag" => 300, "subfield_cd" => ['a', 'b', 'c', 'e']])

                        ->orWhere(["tag" => 541, "subfield_cd" => 'h'])->all();



Which SQL output is




SELECT * FROM `usmarc_subfield_dm` WHERE (((((((((((`tag`=100) AND (`subfield_cd`='a')) OR ((`tag`=650) AND (`subfield_cd`='a'))) OR ((`tag`=250) AND (`subfield_cd`='a'))) OR ((`tag`=10) AND (`subfield_cd`='a'))) OR ((`tag`=20) AND (`subfield_cd`='a'))) OR ((`tag`=50) AND (`subfield_cd` IN ('a', 'b', 'c')))) OR ((`tag`=82) AND (`subfield_cd` IN ('a', '2')))) OR ((`tag`=260) AND (`subfield_cd` IN ('a', 'b', 'c')))) OR ((`tag`=520) AND (`subfield_cd`='a'))) OR ((`tag`=300) AND (`subfield_cd` IN ('a', 'b', 'c', 'e')))) OR ((`tag`=541) AND (`subfield_cd`='h'))



Is so large :P but the issue is the following: in the view, $usmarc variable only has 6 records, but if I run directly the sentence from console I get




[font=monospace]SELECT * FROM `usmarc_subfield_dm` WHERE (((((((((((`tag`=100) AND (`subfield_cd`='a')) OR ((`tag`=650) AND (`subfield_cd`='a'))) OR ((`tag`=250) AND (`subfield_cd`='a'))

) OR ((`tag`=10) AND (`subfield_cd`='a'))) OR ((`tag`=20) AND (`subfield_cd`='a'))) OR ((`tag`=50) AND (`subfield_cd` IN ('a', 'b', 'c')))) OR ((`tag`=82) AND (`subfield_cd` IN ('a', '2')))) O

R ((`tag`=260) AND (`subfield_cd` IN ('a', 'b', 'c')))) OR ((`tag`=520) AND (`subfield_cd`='a'))) OR ((`tag`=300) AND (`subfield_cd` IN ('a', 'b', 'c', 'e')))) OR ((`tag`=541) AND (`subfield_c

d`='h'));

+-----+-------------+--------------------------------------------------+----------------+

| tag | subfield_cd | description                                      | repeatable_flg |

+-----+-------------+--------------------------------------------------+----------------+

|  10 | a           | LC control number                                | N              |

|  20 | a           | International Standard Book Number               | N              |

|  50 | a           | Classification number                            | Y              |

|  50 | b           | Item number                                      | N              |

|  82 | 2           | Edition number                                   | N              |

|  82 | a           | Classification number                            | Y              |

| 100 | a           | Personal name                                    | N              |

| 250 | a           | Edition statement                                | N              |

| 260 | a           | Place of publication, distribution, etc.         | Y              |

| 260 | b           | Name of publisher, distributor, etc.             | Y              |

| 260 | c           | Date of publication, distribution, etc.          | Y              |

| 300 | a           | Extent                                           | Y              |

| 300 | b           | Other physical details                           | N              |

| 300 | c           | Dimensions                                       | Y              |

| 300 | e           | Accompanying material                            | N              |

| 520 | a           | Summary, etc. note                               | N              |

| 541 | h           | Purchase price                                   | N              |

| 650 | a           | Topical term or geographic name as entry element | N              |

+-----+-------------+--------------------------------------------------+----------------+

[b]18 rows in set (0.00 sec)[/b]



[/font]

So the model should return the same results but actually isn’t.

Why I’m getting this behavior?

Kind regards.

Hi Néstor,

Yes, it should. I agree with you.

But what exactly do you mean by "in the view"?

BTW, I’ve noticed that the result could be 6 if you group the records by “subfield_cd” (a, b, c, e, h, 2). Doesn’t it mean something to you?

In the view I mean, I saw only 6 records in the view :P (that was because once I found this issue I ran here to post it), and after checking the model only was returning 6 records in the view when actually the view should have 18 rows.

No, I didn’t grouped the results as you can see in the raw SQL and in the Model builder. The model builder raws that SQL sentence that once is executed returns the correct results but the model isn’t.

Hi Néstor,

Frankly saying, I don’t believe that yii should have such a simple but fatal bug. You should have overlooked something in your view code that might make the difference.

If you do believe it might be a bug in Yii, you should file the bug report to github repo with a minimum code that should reproduce the issue.