Caching Active Record

I’m creating an website to sell tickets from events, when I list an event I will show a list of all tickets related to that event with the name os the users that add that tickets.

At the moment I check on the debugbar and it’s easy to reach 1000 DB requests.

To list the events I use something like this:

Event::find()->with(['tickets', 'user'])->where(['slug' => $slug])->one()

To list all tickets from an event I have this:

foreach ($model->Tickets as $key => $ticket){

 echo $ticket->user->profile->name;


In my website users can make offers for each ticket so the the page is different for each users. What is the best way to caching the query with the list os the tickets and the user name of the person that add that ticket?

I have tried this:

$model = Event::getDb()->cache(function ($db) use($slug) {

            return Event::find()->with(['tickets', 'user'])->where(['slug' => $slug])->one();


But it seams only cache the event not the tickets data neither the user data of each ticket. I know this is a bit complex but its a real case and can be useful to many people