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

时间:2018-11-17 22:58:10

标签: php yii yii2

我有两个带有设计主数据详细信息的表。

主表命名为Format2006FlatFileMaster

详细信息表被命名为Format2006FlatFileDetail

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

我的意思是这样

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

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

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

请告知。

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:1)

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

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

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

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

此致