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 => app\models\utilities\Format2006FlatFileDetail#1
        (
            [yii\db\BaseActiveRecord:_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'
            ]
            [yii\db\BaseActiveRecord:_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'
            ]
            [yii\db\BaseActiveRecord:_related] => []
            [yii\db\BaseActiveRecord:_relationsDependencies] => []
            [yii\base\Model:_errors] => null
            [yii\base\Model:_validators] => null
            [yii\base\Model:_scenario] => 'default'
            [yii\base\Component:_events] => []
            [yii\base\Component:_eventWildcards] => []
            [yii\base\Component:_behaviors] => []
        )
        1 => app\models\utilities\Format2006FlatFileDetail#2
        (
            [yii\db\BaseActiveRecord:_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)'
            ]
            [yii\db\BaseActiveRecord:_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)'
            ]
            [yii\db\BaseActiveRecord:_related] => []
            [yii\db\BaseActiveRecord:_relationsDependencies] => []
            [yii\base\Model:_errors] => null
            [yii\base\Model:_validators] => null
            [yii\base\Model:_scenario] => 'default'
            [yii\base\Component:_events] => []
            [yii\base\Component:_eventWildcards] => []
            [yii\base\Component:_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.

0

2 Answers 2

5

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);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Nice 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,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.