代码之家  ›  专栏  ›  技术社区  ›  Fadly Dzil

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

  •  1
  • Fadly Dzil  · 技术社区  · 7 年前

    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 回复  |  直到 7 年前
        1
  •  3
  •   rob006    7 年前

    你需要使用 ArrayHelper::toArray() 要将对象转换为数组,请执行以下操作:

    $details = ArrayHelper::map(Format2006FlatFileMaster::find()->all(), 'alias', function ($model) {
        return ArrayHelper::toArray($model->format2006FlatFileDetails);
    });
    
        2
  •  1
  •   Anees Hikmat Abu Hmiad    7 年前

    您可以通过在中声明新的关系方法来解决此问题 Format2006FlatFileMaster 有名字 format2006FlatFileDetailsAsArray asArray 阿萨雷

    或者另一种方式: 您可以在使用模型->的嵌套查询中使用Join/JoinWith;关系,通过这种方式可以设置 阿萨雷 直接和得到数组太。。。这样地:

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

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

    当做

    推荐文章