Yii2 在第三个参数上使用 ArrayHelper::map、return child

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

我有两个 table 具有设计大师详细信息。

大师table名叫Format2006FlatFileMaster

详细信息 table 的名称为 Format2006FlatFileDetail

我的目标是,我想用一个字段作为key,value是他们child的master

我的意思是这样

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

我喜欢这个数据,(例如,这是虚拟数据)

'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] => []
        )
    ]
]

可以看到,第三个参数的数据在数组objects中, 我需要这样的数组格式。

'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)'
        ]
    ]
]

请指教

您可以通过在您的 Format2006FlatFileMaster 中声明一个名为 format2006FlatFileDetailsAsArray 的新关系方法来解决此问题,该方法将有一个 asArray,由 asArray关系的响应将根据需要作为数组检索。

或另一种方式: 您可以在使用模型->关系的嵌套查询中使用 Join/JoinWith,这样您就可以直接设置 asArray 并获得数组...像这样:

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

注意:您也可以测试 Format2006FlatFileMaster::find()->asArray()->all(),也许它也能解决您的问题,但我不确定。

此致,

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

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