获取条件为

时间:2016-10-15 12:40:43

标签: php mysql symfony doctrine-orm twig

我正在建立一个性能监控网站。

我有两张桌子:

  • 用户表格以一对多的方式与表演相关联

  • 与用户表多对一链接的效果表

我只是想在我的表演中获得最后一个非空的重量并在树枝中显示

database screenshot 例如,在此数据库中,结果将是:80

我尝试了查询,但收到了一条恐怖信息,所以我不知道该怎么办 在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我希望这可以帮到你。 我想你有一个Performance实体的存储库类。我会将这种代码用于Symfony3应用程序

<?php
namespace AppBundle\Controller\Performance;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use AppBundle\Repository\PerformanceRepository;

class PerformanceController extends Controller
{

    public function lastWeightAction()
    {
        $repo = $this->getDoctrine()->getManager()->getRepository('AppBundle:Performance');
        $lastPerformance = $repo->getLastWeigth();

        //some other code
    }
}

编辑:这是Symfony 3应用程序中控制器的一个使用例:

class PerformanceRepository extends \Doctrine\ORM\EntityRepository
{

    public function getLastWeigth($userId)
    {
       $qb = $this->getEntityManager()->createQueryBuilder()
            ->select('p')
            ->from($this->_entityName, 'p')
            ->join('p.user', 'u')
            ->where('u.id = :userId')
            ->expr()->isNotNull('p.weight')
            ->orderBy('p.date', 'desc') 
            ->setMaxResults(1);
            ->setParameter(':userId', $userId);
       $query = $qb->getQuery();
       $result = $query->getSingleResult();

       return $result;
    }
}

EDIT2: 如果您需要通过用户Id获得最后一个重量:

.box2 {
    margin: 5px 5px 5px 0;
    text-align: center;
    float: left;
    background-color: #FFF;
    color: #000;
    border: 2px solid #A10000;
    height: auto;
    width: 150px;
    font-size: 12px;
 }

.row {
    margin-right: -15px;
    margin-left: -15px;
}

.container {
    padding-right: 15px;
    padding-left: 15px;
    margin-right: auto;
    margin-left: auto;
}