S
S
Sergey Lysogor2017-07-24 20:40:24
Yii
Sergey Lysogor, 2017-07-24 20:40:24

How to make Yii2 get all fields from relationships?

So, given:
* There is a model A, with a specific hasOne relationship to model B. The usual belonging to a specific author.
* There is a REST-endpoint to which the client will come and expect to receive such JSON:

{
    success: true,
    code: 200,
    data: [{
            id: 1, // <- From table A
            title: "TESTTESTTEST", // <- From table A
            description: "LONGTESTDESCRIPTION", // <- From table A
            username: "IvanBEST" // <- From table B and from field "username"
        },
        {
            id: 2, // <- From table A
            title: "Post #777", // <- From table A
            description: "Lorem ipsum...", // <- From table A
            username: "Oleg222" // <- From table B and from field "username"
        },
        {
            ...
        }
    ]
}

There are actually a lot of fields in the model, now I would like to at least teach the model to resolve connections, but there is a problem:
public function actionIndex()
    {
        return MyModel::find()->with(['author'])->all();
    }

Returns the following JSON array:
{
    "success": true,
    "code": 200,
    "data": [
        {
            "id": 1,
            "title": "Faker id5975f7355e568",
            "description": "Voluptas cum incidunt et enim...",
            "author_id": 1, // <--
            "image_id": 1,
            "parent_id": null,
            "created_at": "2017-07-24 16:33:41",
        },
        {
            "id": 2,
            "title": "Faker id5975f7357aa18",
            "description": "Eum dolores eum maxime quos...",
            "author_id": null, // <-- ТУТ ясно
            "image_id": 1,
            "parent_id": null,
            "created_at": "2017-07-24 16:33:41",
        },
]
}

And the desired design:
return Quest::find()->select(['id', 'title', 'description', 'author.username'])->with(['author'])->all();

... In general, it throws an error in which it is clear that Yii2 does not join tables.
I do this and the main problem pops up:
return Quest::find()->alias('t')->select(['t.id', 't.title', 't.description', 'author.username'])->innerJoinWith(['author author'])->all();

...This code creates the following query...
SELECT `t`.`id`, `t`.`title`, `t`.`description`, `author`.`username` FROM `tb__quest` `t` INNER JOIN `tb__user` `author` ON `t`.`author_id` = `author`.`id`

...And everything seems to be fine, but in the answer >>>
{
            "id": 1,
            "title": "Faker id5975f7355e568",
            "description": "Voluptas cum incidunt et enim..."
        },
        {
            "id": 2,
            "title": "Faker id5975f735a8923",
            "description": "Eum delectus quis vero laborum ab..."
        },

Question to colleagues - where is the username field and where is the error????

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim Timofeev, 2017-07-25
@serhioli

If I understand you correctly, then instead ->withof->joinWith

return Quest::find()->select(['id', 'title', 'description', 'author.username'])->joinWith(['author'])->all();

I
Immortal_pony, 2017-07-24
@Immortal_pony

Add to the model
AND specify in the request

->select(['t.id', 't.title', 't.description', 'author.username as "authorName"'])

D
davidnum95, 2017-07-25
@davidnum95

How is the relationship described? In fact, the author should have returned an object, but not an id
EDITED: you have an array in with, but it should just be a stringwith('author')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question