Error getting data from multiple mysql php tables?

2020-05-27 php mysql

Faced such an error as:

Error: Illegal string offset 'id' in

enter image description here

function get_wall(int $owner_id): array {
$db = DbMysqlProvider::getConnection();

$db->where('owner_id', $owner_id);
$db->where('active', 1);
$db->orderBy('date', 'DESC');

$raw_wall = $db->get('wall');

$raw_wall = array_combine(
    array_map(function ($item) { return $item['id']; }, $raw_wall),
    $raw_wall
);

$audios = $db->rawQuery('
    SELECT wall_audios.audio AS audio
    FROM wall_audios
    JOIN wall ON wall.id = wall_audios.wall
    WHERE wall_audios.wall IN (?)', [7]);

foreach ($raw_wall as $raw_wall) {
    $raw_wall['audio'] = $audios;
}

return $raw_wall ?? [];

}

I use the function like this

$wall = get_wall($owner_id);

foreach ($wall as $item) {
$attachments = [];
if (!empty($item['photo'])) {
    $attachments[] = [
        'type'  => 'photo',
        'photo' => get_photo_by_id($item['photo'])
    ];
}

if (!empty($item['audio'])) {

    foreach (get_audio_by_id($item['audio']) as $audio) {
        $attachments[] = [
            'type'  => 'audio',
            'audio' => $audio
        ];
    }

}

$items[] = [
    'id'       => $item['id'],
    'owner_id' => $item['owner_id'],
    'text'     => $item['text'],
    'date'     => $item['date'],
    'likes'    => [
        'count' => 10
    ],
    'views'    => [
        'count' => 543
    ],
    'comments' => [
        'count' => 10
    ],
    'shares'   => [
        'count' => 10
    ],
    'attachments' => $attachments
];
}

With this code, I want to load the data from the wall_audios table into the wall table. And so that the data would be contained in the audio cell I created.

Answers

Related