Sunday, February 13, 2022

[FIXED] Intermittent issue with query builder - Serialization of 'Closure' is not allowed

Issue

In DebugKit, when I click on Variables, the only thing I see is:

Serialization of 'Closure' is not allowed

For instance, the entire code of my index function:

$data = $this->Seasons->find()
->where(['Seasons.user_id' => $this->Auth->user('id')]);

foreach($data as $season){
    $s = $season->id;

    $leagues = $this->Leagues->find()->innerJoinWith('Games', function ($q) use($s) {
            return $q->where(['Games.season_id' => $s]);
        }
    )
    ->where(['Leagues.user_id' => $this->Auth->user('id')])
    ->distinct(['Leagues.name'])
    ->order(['Leagues.name']);

    foreach($leagues as $league){
        $l = $league->id;

        $gametypes = $this->GameTypes->find()->innerJoinWith('Games', function ($q) use($s, $l) {
            return $q->where([
                    'Games.season_id' => $s,
                    'Games.league_id' => $l 
                ]);
            }
        )
        ->distinct(['GameTypes.sort_order'])
        ->order(['GameTypes.sort_order']);

        //set values to data
        $league["game_types"] = $gametypes;

    }
    //set values to data
    $season['leagues'] = $leagues;
}

$this->set(compact('data'));

This is an intermittent issue for me. In some cases, I can see DebugKit's variables just fine, others this error comes along and I have to use debug() to see the content of the variable.

Edit #1

Cake version: '3.3.10'

Edit #2

Code replaced by something that actually does not work. Previous code, it turns out, was working. This, does not.

I'm starting to suspect that this issue crops up whenever I attempt to build Query objects and populate keys with other Query objects?

Edit #3

I think I found the solution. If I call toArray() in all queries, DebugKit will show me the actual content, not the error.

I think my suspicion in Edit #2 was right. That, appending what are essentially Closures into other Closure was what DebugKit didn't like? The question begs, anything wrong with calling toArray() everywhere like I am?


Solution

I had to call toArray() in all my queries.



Answered By - TechFanDan

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.