Yii2 Using ArrayHelper::map, return child on third parameter












1















I have two tables with design master detail.



The master table is named Format2006FlatFileMaster.



The detail table is named Format2006FlatFileDetail.



My goal is, I want to use a field as key and the value is their child of master.



I mean like this



$details = ArrayHelper::map(Format2006FlatFileMaster::find()->all(), 'alias', function ($model) {
$model->format2006FlatFileDetails;
});


I got like this data, (For ex, this is dummy data)



'details' => [
'HDR01' => [
0 => appmodelsutilitiesFormat2006FlatFileDetail#1
(
[yiidbBaseActiveRecord:_attributes] => [
'id' => 1
'2006_flat_file_master_id' => 1
'field_name' => 'Record Lable'
'start' => 1
'width' => 5
'decimal' => null
'type' => 'C'
'mandatory' => 'Y'
'note' => 'HDR01'
]
[yiidbBaseActiveRecord:_oldAttributes] => [
'id' => 1
'2006_flat_file_master_id' => 1
'field_name' => 'Record Lable'
'start' => 1
'width' => 5
'decimal' => null
'type' => 'C'
'mandatory' => 'Y'
'note' => 'HDR01'
]
[yiidbBaseActiveRecord:_related] =>
[yiidbBaseActiveRecord:_relationsDependencies] =>
[yiibaseModel:_errors] => null
[yiibaseModel:_validators] => null
[yiibaseModel:_scenario] => 'default'
[yiibaseComponent:_events] =>
[yiibaseComponent:_eventWildcards] =>
[yiibaseComponent:_behaviors] =>
)
1 => appmodelsutilitiesFormat2006FlatFileDetail#2
(
[yiidbBaseActiveRecord:_attributes] => [
'id' => 2
'2006_flat_file_master_id' => 1
'field_name' => 'Message Function Code'
'start' => 6
'width' => 1
'decimal' => null
'type' => ''
'mandatory' => 'Y'
'note' => '1 Original 2 Update (handle manually) 3 Consolidation (handle manually)'
]
[yiidbBaseActiveRecord:_oldAttributes] => [
'id' => 2
'2006_flat_file_master_id' => 1
'field_name' => 'Message Function Code'
'start' => 6
'width' => 1
'decimal' => null
'type' => ''
'mandatory' => 'Y'
'note' => '1 Original 2 Update (handle manually) 3 Consolidation (handle manually)'
]
[yiidbBaseActiveRecord:_related] =>
[yiidbBaseActiveRecord:_relationsDependencies] =>
[yiibaseModel:_errors] => null
[yiibaseModel:_validators] => null
[yiibaseModel:_scenario] => 'default'
[yiibaseComponent:_events] =>
[yiibaseComponent:_eventWildcards] =>
[yiibaseComponent:_behaviors] =>
)
]
]


As you can see, the data on the third parameter is in array objects,
I need in array format like this.



'details' => [
'HDR01' => [
0 => [
'id' => 1
'2006_flat_file_master_id' => 1
'field_name' => 'Record Lable'
'start' => 1
'width' => 5
'decimal' => null
'type' => 'C'
'mandatory' => 'Y'
'note' => 'HDR01'
],
1 => [
'id' => 2
'2006_flat_file_master_id' => 1
'field_name' => 'Message Function Code'
'start' => 6
'width' => 1
'decimal' => null
'type' => ''
'mandatory' => 'Y'
'note' => '1 Original 2 Update (handle manually) 3 Consolidation (handle manually)'
]
]
]


Please Advise.










share|improve this question



























    1















    I have two tables with design master detail.



    The master table is named Format2006FlatFileMaster.



    The detail table is named Format2006FlatFileDetail.



    My goal is, I want to use a field as key and the value is their child of master.



    I mean like this



    $details = ArrayHelper::map(Format2006FlatFileMaster::find()->all(), 'alias', function ($model) {
    $model->format2006FlatFileDetails;
    });


    I got like this data, (For ex, this is dummy data)



    'details' => [
    'HDR01' => [
    0 => appmodelsutilitiesFormat2006FlatFileDetail#1
    (
    [yiidbBaseActiveRecord:_attributes] => [
    'id' => 1
    '2006_flat_file_master_id' => 1
    'field_name' => 'Record Lable'
    'start' => 1
    'width' => 5
    'decimal' => null
    'type' => 'C'
    'mandatory' => 'Y'
    'note' => 'HDR01'
    ]
    [yiidbBaseActiveRecord:_oldAttributes] => [
    'id' => 1
    '2006_flat_file_master_id' => 1
    'field_name' => 'Record Lable'
    'start' => 1
    'width' => 5
    'decimal' => null
    'type' => 'C'
    'mandatory' => 'Y'
    'note' => 'HDR01'
    ]
    [yiidbBaseActiveRecord:_related] =>
    [yiidbBaseActiveRecord:_relationsDependencies] =>
    [yiibaseModel:_errors] => null
    [yiibaseModel:_validators] => null
    [yiibaseModel:_scenario] => 'default'
    [yiibaseComponent:_events] =>
    [yiibaseComponent:_eventWildcards] =>
    [yiibaseComponent:_behaviors] =>
    )
    1 => appmodelsutilitiesFormat2006FlatFileDetail#2
    (
    [yiidbBaseActiveRecord:_attributes] => [
    'id' => 2
    '2006_flat_file_master_id' => 1
    'field_name' => 'Message Function Code'
    'start' => 6
    'width' => 1
    'decimal' => null
    'type' => ''
    'mandatory' => 'Y'
    'note' => '1 Original 2 Update (handle manually) 3 Consolidation (handle manually)'
    ]
    [yiidbBaseActiveRecord:_oldAttributes] => [
    'id' => 2
    '2006_flat_file_master_id' => 1
    'field_name' => 'Message Function Code'
    'start' => 6
    'width' => 1
    'decimal' => null
    'type' => ''
    'mandatory' => 'Y'
    'note' => '1 Original 2 Update (handle manually) 3 Consolidation (handle manually)'
    ]
    [yiidbBaseActiveRecord:_related] =>
    [yiidbBaseActiveRecord:_relationsDependencies] =>
    [yiibaseModel:_errors] => null
    [yiibaseModel:_validators] => null
    [yiibaseModel:_scenario] => 'default'
    [yiibaseComponent:_events] =>
    [yiibaseComponent:_eventWildcards] =>
    [yiibaseComponent:_behaviors] =>
    )
    ]
    ]


    As you can see, the data on the third parameter is in array objects,
    I need in array format like this.



    'details' => [
    'HDR01' => [
    0 => [
    'id' => 1
    '2006_flat_file_master_id' => 1
    'field_name' => 'Record Lable'
    'start' => 1
    'width' => 5
    'decimal' => null
    'type' => 'C'
    'mandatory' => 'Y'
    'note' => 'HDR01'
    ],
    1 => [
    'id' => 2
    '2006_flat_file_master_id' => 1
    'field_name' => 'Message Function Code'
    'start' => 6
    'width' => 1
    'decimal' => null
    'type' => ''
    'mandatory' => 'Y'
    'note' => '1 Original 2 Update (handle manually) 3 Consolidation (handle manually)'
    ]
    ]
    ]


    Please Advise.










    share|improve this question

























      1












      1








      1








      I have two tables with design master detail.



      The master table is named Format2006FlatFileMaster.



      The detail table is named Format2006FlatFileDetail.



      My goal is, I want to use a field as key and the value is their child of master.



      I mean like this



      $details = ArrayHelper::map(Format2006FlatFileMaster::find()->all(), 'alias', function ($model) {
      $model->format2006FlatFileDetails;
      });


      I got like this data, (For ex, this is dummy data)



      'details' => [
      'HDR01' => [
      0 => appmodelsutilitiesFormat2006FlatFileDetail#1
      (
      [yiidbBaseActiveRecord:_attributes] => [
      'id' => 1
      '2006_flat_file_master_id' => 1
      'field_name' => 'Record Lable'
      'start' => 1
      'width' => 5
      'decimal' => null
      'type' => 'C'
      'mandatory' => 'Y'
      'note' => 'HDR01'
      ]
      [yiidbBaseActiveRecord:_oldAttributes] => [
      'id' => 1
      '2006_flat_file_master_id' => 1
      'field_name' => 'Record Lable'
      'start' => 1
      'width' => 5
      'decimal' => null
      'type' => 'C'
      'mandatory' => 'Y'
      'note' => 'HDR01'
      ]
      [yiidbBaseActiveRecord:_related] =>
      [yiidbBaseActiveRecord:_relationsDependencies] =>
      [yiibaseModel:_errors] => null
      [yiibaseModel:_validators] => null
      [yiibaseModel:_scenario] => 'default'
      [yiibaseComponent:_events] =>
      [yiibaseComponent:_eventWildcards] =>
      [yiibaseComponent:_behaviors] =>
      )
      1 => appmodelsutilitiesFormat2006FlatFileDetail#2
      (
      [yiidbBaseActiveRecord:_attributes] => [
      'id' => 2
      '2006_flat_file_master_id' => 1
      'field_name' => 'Message Function Code'
      'start' => 6
      'width' => 1
      'decimal' => null
      'type' => ''
      'mandatory' => 'Y'
      'note' => '1 Original 2 Update (handle manually) 3 Consolidation (handle manually)'
      ]
      [yiidbBaseActiveRecord:_oldAttributes] => [
      'id' => 2
      '2006_flat_file_master_id' => 1
      'field_name' => 'Message Function Code'
      'start' => 6
      'width' => 1
      'decimal' => null
      'type' => ''
      'mandatory' => 'Y'
      'note' => '1 Original 2 Update (handle manually) 3 Consolidation (handle manually)'
      ]
      [yiidbBaseActiveRecord:_related] =>
      [yiidbBaseActiveRecord:_relationsDependencies] =>
      [yiibaseModel:_errors] => null
      [yiibaseModel:_validators] => null
      [yiibaseModel:_scenario] => 'default'
      [yiibaseComponent:_events] =>
      [yiibaseComponent:_eventWildcards] =>
      [yiibaseComponent:_behaviors] =>
      )
      ]
      ]


      As you can see, the data on the third parameter is in array objects,
      I need in array format like this.



      'details' => [
      'HDR01' => [
      0 => [
      'id' => 1
      '2006_flat_file_master_id' => 1
      'field_name' => 'Record Lable'
      'start' => 1
      'width' => 5
      'decimal' => null
      'type' => 'C'
      'mandatory' => 'Y'
      'note' => 'HDR01'
      ],
      1 => [
      'id' => 2
      '2006_flat_file_master_id' => 1
      'field_name' => 'Message Function Code'
      'start' => 6
      'width' => 1
      'decimal' => null
      'type' => ''
      'mandatory' => 'Y'
      'note' => '1 Original 2 Update (handle manually) 3 Consolidation (handle manually)'
      ]
      ]
      ]


      Please Advise.










      share|improve this question














      I have two tables with design master detail.



      The master table is named Format2006FlatFileMaster.



      The detail table is named Format2006FlatFileDetail.



      My goal is, I want to use a field as key and the value is their child of master.



      I mean like this



      $details = ArrayHelper::map(Format2006FlatFileMaster::find()->all(), 'alias', function ($model) {
      $model->format2006FlatFileDetails;
      });


      I got like this data, (For ex, this is dummy data)



      'details' => [
      'HDR01' => [
      0 => appmodelsutilitiesFormat2006FlatFileDetail#1
      (
      [yiidbBaseActiveRecord:_attributes] => [
      'id' => 1
      '2006_flat_file_master_id' => 1
      'field_name' => 'Record Lable'
      'start' => 1
      'width' => 5
      'decimal' => null
      'type' => 'C'
      'mandatory' => 'Y'
      'note' => 'HDR01'
      ]
      [yiidbBaseActiveRecord:_oldAttributes] => [
      'id' => 1
      '2006_flat_file_master_id' => 1
      'field_name' => 'Record Lable'
      'start' => 1
      'width' => 5
      'decimal' => null
      'type' => 'C'
      'mandatory' => 'Y'
      'note' => 'HDR01'
      ]
      [yiidbBaseActiveRecord:_related] =>
      [yiidbBaseActiveRecord:_relationsDependencies] =>
      [yiibaseModel:_errors] => null
      [yiibaseModel:_validators] => null
      [yiibaseModel:_scenario] => 'default'
      [yiibaseComponent:_events] =>
      [yiibaseComponent:_eventWildcards] =>
      [yiibaseComponent:_behaviors] =>
      )
      1 => appmodelsutilitiesFormat2006FlatFileDetail#2
      (
      [yiidbBaseActiveRecord:_attributes] => [
      'id' => 2
      '2006_flat_file_master_id' => 1
      'field_name' => 'Message Function Code'
      'start' => 6
      'width' => 1
      'decimal' => null
      'type' => ''
      'mandatory' => 'Y'
      'note' => '1 Original 2 Update (handle manually) 3 Consolidation (handle manually)'
      ]
      [yiidbBaseActiveRecord:_oldAttributes] => [
      'id' => 2
      '2006_flat_file_master_id' => 1
      'field_name' => 'Message Function Code'
      'start' => 6
      'width' => 1
      'decimal' => null
      'type' => ''
      'mandatory' => 'Y'
      'note' => '1 Original 2 Update (handle manually) 3 Consolidation (handle manually)'
      ]
      [yiidbBaseActiveRecord:_related] =>
      [yiidbBaseActiveRecord:_relationsDependencies] =>
      [yiibaseModel:_errors] => null
      [yiibaseModel:_validators] => null
      [yiibaseModel:_scenario] => 'default'
      [yiibaseComponent:_events] =>
      [yiibaseComponent:_eventWildcards] =>
      [yiibaseComponent:_behaviors] =>
      )
      ]
      ]


      As you can see, the data on the third parameter is in array objects,
      I need in array format like this.



      'details' => [
      'HDR01' => [
      0 => [
      'id' => 1
      '2006_flat_file_master_id' => 1
      'field_name' => 'Record Lable'
      'start' => 1
      'width' => 5
      'decimal' => null
      'type' => 'C'
      'mandatory' => 'Y'
      'note' => 'HDR01'
      ],
      1 => [
      'id' => 2
      '2006_flat_file_master_id' => 1
      'field_name' => 'Message Function Code'
      'start' => 6
      'width' => 1
      'decimal' => null
      'type' => ''
      'mandatory' => 'Y'
      'note' => '1 Original 2 Update (handle manually) 3 Consolidation (handle manually)'
      ]
      ]
      ]


      Please Advise.







      php yii yii2






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 17 '18 at 22:58









      Fadly DzilFadly Dzil

      8151540




      8151540
























          2 Answers
          2






          active

          oldest

          votes


















          3














          You need to use ArrayHelper::toArray() to convert object to array:



          $details = ArrayHelper::map(Format2006FlatFileMaster::find()->all(), 'alias', function ($model) {
          return ArrayHelper::toArray($model->format2006FlatFileDetails);
          });





          share|improve this answer
























          • Nice answer..:)

            – Anees Hikmat Abu Hmiad
            Nov 19 '18 at 13:27



















          1














          you can resolve this issue by declare a new relation method in your Format2006FlatFileMaster with name format2006FlatFileDetailsAsArray, and this method will has an asArray, by asArray the response for relation will retrieve as an array as you want.



          or another way:
          You can use Join/JoinWith in query nested of using model->relation, in this way you can set asArray direct and got array too... like this:



          $query = yourModel::find()
          ->joinWith([....])
          ->andWhere(....)
          ->asArray()->all();


          Note: You can test Format2006FlatFileMaster::find()->asArray()->all() too, maybe its will resolve your issue too, but Im not sure.



          Regards,






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53356345%2fyii2-using-arrayhelpermap-return-child-on-third-parameter%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            You need to use ArrayHelper::toArray() to convert object to array:



            $details = ArrayHelper::map(Format2006FlatFileMaster::find()->all(), 'alias', function ($model) {
            return ArrayHelper::toArray($model->format2006FlatFileDetails);
            });





            share|improve this answer
























            • Nice answer..:)

              – Anees Hikmat Abu Hmiad
              Nov 19 '18 at 13:27
















            3














            You need to use ArrayHelper::toArray() to convert object to array:



            $details = ArrayHelper::map(Format2006FlatFileMaster::find()->all(), 'alias', function ($model) {
            return ArrayHelper::toArray($model->format2006FlatFileDetails);
            });





            share|improve this answer
























            • Nice answer..:)

              – Anees Hikmat Abu Hmiad
              Nov 19 '18 at 13:27














            3












            3








            3







            You need to use ArrayHelper::toArray() to convert object to array:



            $details = ArrayHelper::map(Format2006FlatFileMaster::find()->all(), 'alias', function ($model) {
            return ArrayHelper::toArray($model->format2006FlatFileDetails);
            });





            share|improve this answer













            You need to use ArrayHelper::toArray() to convert object to array:



            $details = ArrayHelper::map(Format2006FlatFileMaster::find()->all(), 'alias', function ($model) {
            return ArrayHelper::toArray($model->format2006FlatFileDetails);
            });






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 18 '18 at 11:03









            rob006rob006

            9,38131032




            9,38131032













            • Nice answer..:)

              – Anees Hikmat Abu Hmiad
              Nov 19 '18 at 13:27



















            • Nice answer..:)

              – Anees Hikmat Abu Hmiad
              Nov 19 '18 at 13:27

















            Nice answer..:)

            – Anees Hikmat Abu Hmiad
            Nov 19 '18 at 13:27





            Nice answer..:)

            – Anees Hikmat Abu Hmiad
            Nov 19 '18 at 13:27













            1














            you can resolve this issue by declare a new relation method in your Format2006FlatFileMaster with name format2006FlatFileDetailsAsArray, and this method will has an asArray, by asArray the response for relation will retrieve as an array as you want.



            or another way:
            You can use Join/JoinWith in query nested of using model->relation, in this way you can set asArray direct and got array too... like this:



            $query = yourModel::find()
            ->joinWith([....])
            ->andWhere(....)
            ->asArray()->all();


            Note: You can test Format2006FlatFileMaster::find()->asArray()->all() too, maybe its will resolve your issue too, but Im not sure.



            Regards,






            share|improve this answer




























              1














              you can resolve this issue by declare a new relation method in your Format2006FlatFileMaster with name format2006FlatFileDetailsAsArray, and this method will has an asArray, by asArray the response for relation will retrieve as an array as you want.



              or another way:
              You can use Join/JoinWith in query nested of using model->relation, in this way you can set asArray direct and got array too... like this:



              $query = yourModel::find()
              ->joinWith([....])
              ->andWhere(....)
              ->asArray()->all();


              Note: You can test Format2006FlatFileMaster::find()->asArray()->all() too, maybe its will resolve your issue too, but Im not sure.



              Regards,






              share|improve this answer


























                1












                1








                1







                you can resolve this issue by declare a new relation method in your Format2006FlatFileMaster with name format2006FlatFileDetailsAsArray, and this method will has an asArray, by asArray the response for relation will retrieve as an array as you want.



                or another way:
                You can use Join/JoinWith in query nested of using model->relation, in this way you can set asArray direct and got array too... like this:



                $query = yourModel::find()
                ->joinWith([....])
                ->andWhere(....)
                ->asArray()->all();


                Note: You can test Format2006FlatFileMaster::find()->asArray()->all() too, maybe its will resolve your issue too, but Im not sure.



                Regards,






                share|improve this answer













                you can resolve this issue by declare a new relation method in your Format2006FlatFileMaster with name format2006FlatFileDetailsAsArray, and this method will has an asArray, by asArray the response for relation will retrieve as an array as you want.



                or another way:
                You can use Join/JoinWith in query nested of using model->relation, in this way you can set asArray direct and got array too... like this:



                $query = yourModel::find()
                ->joinWith([....])
                ->andWhere(....)
                ->asArray()->all();


                Note: You can test Format2006FlatFileMaster::find()->asArray()->all() too, maybe its will resolve your issue too, but Im not sure.



                Regards,







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 18 '18 at 6:07









                Anees Hikmat Abu HmiadAnees Hikmat Abu Hmiad

                1,582927




                1,582927






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53356345%2fyii2-using-arrayhelpermap-return-child-on-third-parameter%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    How to pass form data using jquery Ajax to insert data in database?

                    National Museum of Racing and Hall of Fame

                    Guess what letter conforming each word