繁体   English   中英

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

[英]Yii2 Using ArrayHelper::map, return child on third parameter

我有两张桌子,上面有设计大师的详细信息。

主表名为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)'
        ]
    ]
]

请指教。

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

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

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

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

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

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

问候,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM