yii2 : Why all() method not return duplicate?

why following test return two different result ?

Query 1


print_r(

	ShopClass\ShopProductSurvey::find()

		->select('survey.id,survey.question,answer.id answer_id,answer.answer')

		->from(ShopClass\ShopProductSurvey::TBL_NAME.' survey')

		->innerJoin(ShopClass\ShopProductSurveyRel::TBL_NAME.' survey_rel','survey_rel.survey_id = survey.id && survey_rel.product_id = :pid',[':pid' => 1])

		->innerJoin(ShopClass\ShopProductSurveyAnswer::TBL_NAME.' answer','answer.survey_id = survey.id')

		->where([

			'survey.enable' => 1,

			'answer.enable' => 1,

		])

		->orderBy('survey_rel.position , answer.position')

		->asArray()

	->all()

);

Query 1 Result




Array

(

    [0] => Array

        (

            [id] => 1

            [question] => Question 1 ?

            [answer_id] => 1

            [answer] => Good

        )


    [1] => Array

        (

            [id] => 2

            [question] => Question 2 ?

            [answer_id] => 5

            [answer] => Nice

        )


)



Query 2




print_r(

	ShopClass\ShopProductSurvey::find()

		->select('survey.id,survey.question,answer.id answer_id,answer.answer')

		->from(ShopClass\ShopProductSurvey::TBL_NAME.' survey')

		->innerJoin(ShopClass\ShopProductSurveyRel::TBL_NAME.' survey_rel','survey_rel.survey_id = survey.id && survey_rel.product_id = :pid',[':pid' => 1])

		->innerJoin(ShopClass\ShopProductSurveyAnswer::TBL_NAME.' answer','answer.survey_id = survey.id')

		->where([

			'survey.enable' => 1,

			'answer.enable' => 1,

		])

		->orderBy('survey_rel.position , answer.position')

		->asArray()

	->createCommand()->queryAll()

);



Query 2 Result




Array

(

    [0] => Array

        (

            [id] => 1

            [question] => Question 1 ?

            [answer_id] => 1

            [answer] => Good

        )


    [1] => Array

        (

            [id] => 1

            [question] => Question 1 ?

            [answer_id] => 2

            [answer] => Nice

        )


    [2] => Array

        (

            [id] => 1

            [question] => Question 1 ?

            [answer_id] => 3

            [answer] => Medium

        )


    [3] => Array

        (

            [id] => 1

            [question] => Question 1 ?

            [answer_id] => 4

            [answer] => Bad

        )


    [4] => Array

        (

            [id] => 2

            [question] => Question 2 ?

            [answer_id] => 5

            [answer] => Nice

        )


    [5] => Array

        (

            [id] => 2

            [question] => Question 2 ?

            [answer_id] => 6

            [answer] => Good

        )


    [6] => Array

        (

            [id] => 2

            [question] => Question 2 ?

            [answer_id] => 7

            [answer] => Medium

        )


    [7] => Array

        (

            [id] => 2

            [question] => Question 2 ?

            [answer_id] => 8

            [answer] => Bad

        )


    [8] => Array

        (

            [id] => 2

            [question] => Question 2 ?

            [answer_id] => 9

            [answer] => Very Bad

        )


)



https://github.com/yiisoft/yii2/issues/13443

Additional info

| Q | A

| ---------------- | —

| Yii version | 2.0.10

| PHP version | 5.6.3

Because in 1st case you’re still working with AR and in 2nd case you’re working with plain SQL query. In 1st case Yii is trying to maintain AR relations hierarchy so you’re immediately getting 1st level of models and relations are hidden deep within these. In 2nd case you’re getting raw rows as is.

Yes, it is logical.

but when set asArray() in 1st, do this operation([size="2"][color="#C0C0C0"]Yii is trying to maintain AR relations hierar…[/color][/size]) is needed ?

It is converting resulting ARs to array. If you need to fetch rows, use query builder.