Search code examples
phpsymfonydqldoctrine-query

DQL Function inside Switch Statement Returns 0 on var_dump, but DQL Function works perfectly when not in Switch Statement


I've got this Doctrine DQL query that works perfectly inside another function, but when I put it inside my switch statement, it returns 0, whether as array or as int. I can't for the life of me figure out why this is the case as it works perfectly otherwise.

By the way, when $user is var_dumped it returns the current user, so that's not the issue.

Here's the DQL:

/**
 * @param User $user
 * @return array
 */
public function getStatsByDQL(User $user)
{
    $parameters = array(
         'user' => $user, 'date' => $this->midNight
    );

    $em2 = $this->getDoctrine()->getManager()
        ->getRepository('AppBundle:ExerciseStats')
        ->createQueryBuilder('g')
        ->select('g.press_over_head_1_weight')
        ->setMaxResults(1)
        ->join('g.user', 'user')
        ->where('user = :user', 'g.timestamp < :date')
        ->orderBy('g.press_over_head_1_weight','DESC')
        ->getQuery()->setParameters($parameters)-
>getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
    return $em2;
}

Here's part of the Switch:

public function getExerciseWeightAndReps(string $exSet, int $setReps)
{
    $user = $this->getUser();
    $w = 0;
    $lastreps = 0;
    switch ($exSet) {

case 'poh1':
            $w = $this->getStatsByDQL($user);
            var_dump($w);
            $lastreps = $this->getUser()->getExerciseStats()->last()
            ->getPressOverHead1Reps();

The var_dump returns 0, whether it is array or by (int)$this->getStatsBy DQL($user);

And like I said, in another function which I created just to test, it works perfectly. Both the DQL function as well as the case are in the default controller, as is the test function.

Any ideas?


Solution

  • What is in $this->midnight ?

    Did you set it somewhere before entering the case statement?