Yii2 使用 ArrayHelper::map,在第三个参数上返回子级

Fad*_*zil 1 php yii yii2

我有两张带有设计主细节的桌子。

主表名为Format2006FlatFileMaster

明细表名为Format2006FlatFileDetail

我的目标是,我想使用一个字段作为键,值是他们的 master 的孩子。

我的意思是这样

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

我得到这样的数据,(例如,这是虚拟数据)

'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] => []
        )
    ]
]
Run Code Online (Sandbox Code Playgroud)

如您所见,第三个参数的数据位于数组对象中,我需要像这样的数组格式。

'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)'
        ]
    ]
]
Run Code Online (Sandbox Code Playgroud)

请指教。

rob*_*006 5

您需要使用ArrayHelper::toArray()将对象转换为数组:

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