catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.84k stars 1.15k forks source link

[New Feature] Data mapping #407

Closed catfan closed 7 years ago

catfan commented 8 years ago

We currently added a new data mapping feature on develop branch here: 6ab60380.

For most case, the data construction we need from database is multi-dimensionally complex. We have to process the multidimensional array from we fetched and them output to the client.

But now, you can describe the column as map on select() columns argument. Medoo will automatically process the data mapping for you.

$data = $database->select('post', [
    '[>]account' => 'user_id'
], [
    'post.content',
    'post.post_id',

    // The key name - profile - will not treat as column.
    'profile' => [
        'account.user_id',
        'account.email'
    ]
]);

echo json_encode( $data );

The returned data will be:

[
    {
        content: "Hello world!",
        post_id: "1",
        profile: {
            user_id: "1",
            email: "foo@bar.com"
        }
    },
    {
        content: "Good day today.",
        post_id: "2",
        profile: {
            user_id: "2",
            email: "abc@example.com"
        }
    }
]

What do you think about it? Leave a comment and idea is welcome.

AndreiTelteu commented 8 years ago

Awesome. I tried it and it's awesome. But it makes conflict when the table that i make a join with has a column the same as my table's column. You have to implement alias in data mapping :)

piotrostrowski commented 8 years ago

Guys, have you found any solution to the @AndreiTelteu issue? Im facing it right now and I have no idea how to deal with it :(