1

I am trying to get these data below,

Table relations:

people(one) <---> (many) people_info (one) <---> (many) people_contact

in the following format,

people: {
    p_id: 10,
    p_price: 3.99,
    people_info : [
        {
            pl_id: 3,
            pl_state: 2,
            pl_district: 6,
            pl_latitude: 6.323434,
            pl_longitude: 108.23499,
            people_contact: [
                {
                    plc_id: 2
                },
                {
                    plc_id: 1
                }
            ]
        },
        {
            pl_id: 2,
            pl_state: 7,
            pl_district: 12,
            pl_latitude: 6.000434,
            pl_longitude: 108.9910003,
            people_contact: [
                {
                    plc_id: 5
                },
                {
                    plc_id: 9
                }
            ]
        }
    ]
}

Currently with these controller codes,

    class PeopleController extends Controller
{
public function actionPeople($params){
    Yii::$app->response->format = Response::FORMAT_JSON;

....//some other codes//.....       
        $people= People::find()->select(['p_id', 'p_price'] )->where(['p_id' => $itemId])->one();
        $info= PeopleContact::find()->with(['plPeople'])->asArray([])->all();

     return array(
        'people' => $people,
        'info' => $info,
     );
}

 }

I got these,

"people": {
    "p_id": "3",
    "p_price": "32.42"
}, "locations": [{
    "pl_id": "1",
    "pl_people": "3",
    "pl_title": "",
    "pl_latitude": "6.16438700000000000000",
    "pl_longitude": "102.28314649999993000000",
    "pl_place": null,
    "pl_premise": null,
    "pl_street": "1",
    "pl_area": "1",
    "pl_postcode": "1",
    "pl_district": "1",
    "pl_state": "3",
    "pl_country": 1,
    "place": null,
    "premise": null,
    "street": null,
    "area": null,
    "postcode": null,
    "district": null,
    "state": null,
    "country": "United Kingdom",
    "contacts": [{
        "plc_name": "joe",
        "plc_phone": "123456",
        "plc_email": null
    }]
}]
}

How do I achieve it in the format mentioned at the top?

1 Answer 1

1
$output;

$people=People::find()->select(['p_id', 'p_price'] )->asArray()->all();
foreach($people as $person) {
    $infos = PersonInfo::find()->where(['person_id' => $person->id])->asArray()->all();
    foreach($infos as $info) {
        $contacts = PersonContact::find()->where(['person_info_id' => $info->id])->asArray()->all();
        foreach($contacts as $contact) {
            $info['contacts'][] = $contact;
        }
        $person['info'][] = $info
    }
    $output['people'][] = $person
}

return $output;

You should loop through and fetch data like this: people > info > contact each next level relying on info fetched from the previous one. Then store it in the format you want such as demonstrated above.

This will output something like:

"people": [{
    ...
    "info": [{
        ...
        "contacts": [{
            ...
        },{
            ...
        }]
    }]
},{
    ...
}]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.