当每个用户有多行时,使用Doctrine获取db中的最后一个用户条目

时间:2017-05-10 05:30:50

标签: php symfony doctrine-orm

我正在尝试为用户获取数据库中的最后一个条目,以便使用我的程序中的数据进行计算。使用last()方法在此实例中不起作用,因为last()将返回null值。请继续阅读,我会进一步解释。在这里查看我的控制器:

/**
 *
 * @Route("user/PyramidPressOverHead", name="user_PyramidPressOverHead")
 * @param Request $request
 * @return \Symfony\Component\HttpFoundation\Response
 *
 */
public function pyramidPressOverHead(Request $request)
{
    $exercisestats = $this->getExerciseStatsByCurrentUser();
    $newwtpoh15 = $this->getExerciseWeightAndReps('poh1',15);
    $newwtpoh12 = $this->getExerciseWeightAndReps('poh2',12);
    $newwtpoh10 = $this->getExerciseWeightAndReps('poh3',10);
    $newwtpoh8 = $this->getExerciseWeightAndReps('poh4',8);
    $newwtpoh6 = $this->getExerciseWeightAndReps('poh5',6);

    $form = $this->createForm('AppBundle\Form\PressOverHeadStatsType', $exercisestats);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $user = $this->getUser();
        $exercisestats->setUserId($this->getUser());
        $exercisestats->setTimestamp(date('Y-m-d H:i:s'));
        $em->persist($exercisestats);
        $em->persist($user);
        $em->flush();

        return $this->redirectToRoute('user_PyramidTricepsExtension');
    }

    return $this->render('user/PyramidPressOverHead.html.twig', array(
        'exercisestats' => $exercisestats,
        'form' => $form->createView(),
        'poh15' => $newwtpoh15, 'poh12' => $newwtpoh12, 'poh10' => $newwtpoh10,
        'poh8' => $newwtpoh8, 'poh6' => $newwtpoh6,
    ));
}

控制器并没有真正显示提交的内容。树枝在这里​​给你更多:

{% extends 'base.html.twig' %}
{% block body %}
<head>


    {% block stylesheets %}<link href="{{ asset('css/custom.css') }}" 
    rel="stylesheet" media="screen">{% endblock %}
    </head>
<body background="{{ asset('sport-1244925_1920.jpg') }}">

{{ form_start(form, { 'style': 'horizontal', 'col_size': 'xs' }) }}
<h1>Press Over Head</h1>
<h3>Your Weight for Set One is {{ poh15 }}</h3>
{{ form_row(form.press_over_head_1_reps) }}
{{ form_row(form.press_over_head_1_weight) }}
<h3>Your Weight for Set two is {{ poh12 }}</h3>
{{ form_row(form.press_over_head_2_reps) }}
{{ form_row(form.press_over_head_2_weight) }}
<h3>Your Weight for Set three is {{ poh10 }}</h3>
{{ form_row(form.press_over_head_3_reps) }}
{{ form_row(form.press_over_head_3_weight) }}
<h3>Your Weight for Set four is {{ poh8 }}</h3>
{{ form_row(form.press_over_head_4_reps) }}
{{ form_row(form.press_over_head_4_weight) }}
<h3>Your Weight for Set five is {{ poh6 }}</h3>
{{ form_row(form.press_over_head_5_reps) }}
{{ form_row(form.press_over_head_5_weight) }}

<h2>Before hitting the submit button, please check over all of your
numbers to ensure they are accurate.</h2>
{{ form_row(form.submit) }}


{% endblock %}

该人输入他们做了多少代表以及每次锻炼的重量。当他们点击提交按钮时,它会提交给数据库。该表中有大约40列。它一次填充用户的统计数据。提交一个练习时,在提交完成后,那些没有数据的列将变为null。这是我的实体的片段:

/**
 * @var int
 * @ORM\Column(name="press_over_head_1_reps", type="smallint", length=6,
 *     nullable=true)
 */
private $press_over_head_1_reps;

/**
 * @var int
 * @ORM\Column(name="press_over_head_1_weight", type="smallint", length=6,
 *     nullable=true)
 */
private $press_over_head_1_weight;

/**
 * @var int
 * @ORM\Column(name="press_over_head_2_reps", type="smallint", length=6,
 *     nullable=true)
 */
private $press_over_head_2_reps;

/**
 * @var int
 * @ORM\Column(name="press_over_head_2_weight", type="smallint", length=6,
 *     nullable=true)
 */
private $press_over_head_2_weight;

/**
 * @var int
 * @ORM\Column(name="press_over_head_3_reps", type="smallint", length=6,
 *     nullable=true)
 */
private $press_over_head_3_reps;

/**
 * @var int
 * @ORM\Column(name="press_over_head_3_weight", type="smallint", length=6,
 *     nullable=true)
 */
private $press_over_head_3_weight;

/**
 * @var int
 * @ORM\Column(name="press_over_head_4_reps", type="smallint", length=6,
 *     nullable=true)
 */
private $press_over_head_4_reps;

/**
 * @var int
 * @ORM\Column(name="press_over_head_4_weight", type="smallint", length=6,
 *     nullable=true)
 */
private $press_over_head_4_weight;

/**
 * @var int
 * @ORM\Column(name="press_over_head_5_reps", type="smallint", length=6,
 *     nullable=true)
 */
private $press_over_head_5_reps;

/**
 * @var int
 * @ORM\Column(name="press_over_head_5_weight", type="smallint", length=6,
 *     nullable=true)
 */
private $press_over_head_5_weight;

我将实体中的所有内容设置为nullable = true,因为如果在没有获取数据的字段中完成提交而没有nullable = true,则会抛出错误。

该程序会跟踪此人的统计信息,因此每次锻炼时,该锻炼日都会有一个新行。因此,在我的控制器中尝试使用它时:

$this->getUser()->getExerciseStats()->last()->getPressOverHead2Reps();

它返回null。 Press Over Head是例程中的第二个练习。由于第一个练习已经提交,它渲染所有空白字段为null,因此第一个练习后的每个练习都返回null,但我需要db中的最后一个实际条目进行计算并填充树枝。

这是我的用户实体:

/**
 * @ORM\OneToMany(targetEntity="AppBundle\Entity\ExerciseStats", 
mappedBy="user_id")
 */
private $exerciseStats;

/**
 * @return mixed
 */
public function getExerciseStats()
{
    return $this->exerciseStats;
}

有没有人对如何获取用户在数据库中输入的最后一行的数据有任何想法?

如果有人需要澄清,请告诉我。感谢您抽出宝贵时间来检查这一点。

1 个答案:

答案 0 :(得分:0)

我用dql查询解决了这个问题,准确地返回了我正在寻找的内容:

public function getStatsByDQL(User $user,  String $col)
{

$midNight = date_create('00:00:00')->format('Y-m-d H:i:s');
$parameters = (array('user' => $user, 'date' => $midNight, 

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