endless commentary with AR & REST

Hi I gave my REST application a commentary function and I am not sure if my code couldn’t be more effective? I am happy if anybody sees something to improve :)

This Code produces an output that gives 2 tiers of comments 10 in the first tier and 2 if there is a second tier. If there are more tiers or more comments on tier 2 the REST Route is called again at this position to populate the DOM.

The discussContent comes from an noSQL DB and is controlled by an SQL with the Table Discuss.

I call the table Discuss 3 times with different query operations.Each powered by an foreach with different input, so is it smart that way?




    private function findDiscussDiscussion($id, $offset)

    {

        $table = Discuss::find()

            ->where([

                'refer_id' => $id,

                'refer_type' => 'discussion'

            ])

            ->with('discussContent')

            ->orderBy(('discuss.id ASC'))

            ->limit(10)

            ->offset($offset)

            ->all();

        foreach ($table as $item) {

            $tier1_table = Discuss::find()

                ->where([

                    'refer_id' => $item['id'],

                    'refer_type' => 'discussion'

                ])

                ->with('discussContent')

                ->limit(2)

                ->offset(0)

                ->all();

            if (isset($tier1_table)) {

                foreach ($tier1_table as $tier1_item) {

                    $tier2_table = Discuss::find()

                        ->where([

                            'refer_id' => $tier1_item['id'],

                            'refer_type' => 'discussion'

                        ])

                        ->count();

                    $discussion[] = array(

                        'id' => $tier1_item['id'],

                        'title' => $tier1_item->discussContent['title'],

                        'content' => $tier1_item->discussContent['content'],

                        'author' => $tier1_item->discussContent['author'],

                        'tags' => $tier1_item->discussContent['tags'],

                        'mentions' => $tier1_item->discussContent['mentions'],

                        'refer' => $tier1_item->discussContent['refer'],

                        'discussion' => $tier2_table

                    );

                }

            } else { $discussion = 0; }

            $result[] = array(

                'id' => $item['id'],

                'title' => $item->discussContent['title'],

                'content' => $item->discussContent['content'],

                'author' => $item->discussContent['author'],

                'tags' => $item->discussContent['tags'],

                'mentions' => $item->discussContent['mentions'],

                'refer' => $item->discussContent['refer'],

                'discussion' => $discussion

            );

        }

        return isset($result) ? $result : null;

    }