php - Symfony 3 从现在开始找到最近的日期

标签 php mysql symfony

我有三个数据库表。用户、 session 、组。 session 和组表是来自用户表的多对多关联。
所以我可以执行 user->getmeetings()user->getgroups()

我只想为用户安排下一次 session ,但我不知道如何实现。无论是通过 SQL 查询还是仅仅在 Controller 中。这是我到目前为止所做的。

$loggedInUser = $em->getRepository('AppBundle\Entity\User')
        ->find($id);

    foreach ($loggedInUser->getMeetings() as $userMeetings) {
        $nextMeeting[$userMeetings->getId()]['id'] = $userMeetings->getId();
        $nextMeeting[$userMeetings->getId()]['group'] = $userMeetings->getGroup();
        $nextMeeting[$userMeetings->getId()]['name'] = $userMeetings->getName();
        $nextMeeting[$userMeetings->getId()]['info'] = $userMeetings->getInfo();
        $nextMeeting[$userMeetings->getId()]['bring'] = $userMeetings->getBring();
        $nextMeeting[$userMeetings->getId()]['summary'] = $userMeetings->getSummary();
        $nextMeeting[$userMeetings->getId()]['files'] = $userMeetings->getFiles();
        $nextMeeting[$userMeetings->getId()]['meetingDate'] = $userMeetings->getMeetingDate();
        $nextMeeting[$userMeetings->getId()]['meetingAddress'] = $userMeetings->getMeetingAddress();
        $nextMeeting[$userMeetings->getId()]['time_diff'] = date_diff($userMeetings->getMeetingDate(), new \DateTime());
    }

我在新数组中添加了一个time_diff 字段来计算从现在到 session 时间的时间。我现在需要做的就是选择最小的一个。
我该怎么做?谢谢。

更新 - 下面是我的用户存储库

namespace AppBundle\Repository;

use AppBundle\Entity\User;
use AppBundle\Entity\Meeting;
use Doctrine\ORM\EntityRepository;
use Doctrine\Common\Collections\Criteria;

class UserRepository extends EntityRepository
{
    public function getComingMeeting()
    {
        $criteria = Criteria::create()
            ->where(Criteria::expr()->gte("meetingDate", new \DateTime('now')))
            ->orderBy(array("meetingDate" => Criteria::ASC))
            ->setMaxResults(1);
    $this->getMeetings()->matching($criteria);
}
}

下面是我的用户实体

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
 * @ORM\Table(name="user")
 */
class User
{

/**
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * @ORM\Column(type="string", nullable=false)
 */
private $first_name;

/**
 * @ORM\Column(type="string", nullable=false)
 */
private $last_name;

/**
 * @ORM\Column(type="string", nullable=false, unique=true)
 */
private $email_address;

/**
 * @ORM\Column(type="string", nullable=true)
 */
private $phone_number;

/**
 * @ORM\Column(type="string", nullable=false)
 */
private $password;

/**
 * @ORM\Column(type="datetime", nullable=true)
 */
private $last_login;

/**
 * @ORM\Column(type="string", nullable=true)
 */
private $reset_password;

/**
 * @ORM\ManyToMany(targetEntity="Meeting", inversedBy="users")
 */
private $meetings;

/**
 * @ORM\ManyToMany(targetEntity="Group", inversedBy="users")
 */
private $groups;

public function __construct()
{
    $this->groups = new arrayCollection();
    $this->meetings = new arrayCollection();
}

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

/**
 * @param mixed $first_name
 */
public function setFirstName($first_name)
{
    $this->first_name = $first_name;
}

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

/**
 * @param mixed $last_name
 */
public function setLastName($last_name)
{
    $this->last_name = $last_name;
}

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

/**
 * @param mixed $email_address
 */
public function setEmailAddress($email_address)
{
    $this->email_address = $email_address;
}

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

/**
 * @param mixed $phone_number
 */
public function setPhoneNumber($phone_number)
{
    $this->phone_number = $phone_number;
}

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

/**
 * @param mixed $password
 */
public function setPassword($password)
{
    $this->password = $password;
}

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

/**
 * @param mixed $last_login
 */
public function setLastLogin($last_login)
{
    $this->last_login = $last_login;
}

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

/**
 * @param mixed $reset_password
 */
public function setResetPassword($reset_password)
{
    $this->reset_password = $reset_password;
}

/**
 * @return arrayCollection|Meeting[]
 */
public function getMeetings()
{
    return $this->meetings;
}

/**
 * @return ArrayCollection|Group[]
 */
public function getGroups()
{
    return $this->groups;
}

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



}

下面是我的 HomeControlller.php

class HomeController extends Controller
{

/**
 * @Route("/home", name="home_show")
 */
public function showAction()

{
    $em = $this->getDoctrine()->getManager();

    //logged in user
    $id = 1;

    $loggedInUser = $em->getRepository('AppBundle\Entity\User')
        ->find($id);

    foreach ($loggedInUser->getMeetings() as $userMeetings) {
        $nextMeeting[$userMeetings->getId()]['id'] = $userMeetings->getId();
        $nextMeeting[$userMeetings->getId()]['group'] = $userMeetings->getGroup();
        $nextMeeting[$userMeetings->getId()]['name'] = $userMeetings->getName();
        $nextMeeting[$userMeetings->getId()]['info'] = $userMeetings->getInfo();
        $nextMeeting[$userMeetings->getId()]['bring'] = $userMeetings->getBring();
        $nextMeeting[$userMeetings->getId()]['summary'] = $userMeetings->getSummary();
        $nextMeeting[$userMeetings->getId()]['files'] = $userMeetings->getFiles();
        $nextMeeting[$userMeetings->getId()]['meetingDate'] = $userMeetings->getMeetingDate();
        $nextMeeting[$userMeetings->getId()]['meetingAddress'] = $userMeetings->getMeetingAddress();
        $nextMeeting[$userMeetings->getId()]['time_diff'] = date_diff($userMeetings->getMeetingDate(), new \DateTime());
    }


    $groups = $em->getRepository('AppBundle\Entity\Group')
        ->findAll();

    return $this->render('home/show.html.twig', [
        'loggedInUser' => $loggedInUser,
        'groups' => $groups,
    ]);
}

}

最佳答案

您可以在您的 UserEntity 中添加一个方法,该方法与 session 集合的匹配条件

namespace AppBundle\Entity;

use Doctrine\Common\Collections\Criteria;

....

public function getComingMeeting()
{
   $criteria = Criteria::create()
     ->where(Criteria::expr()->gte("meetingDate", new \DateTime('now')))
     ->orderBy(array("meetingDate" => Criteria::ASC))
     ->setMaxResults(1);

  return $this->getMeetings()->matching($criteria);
}

参见 http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/working-with-associations.html

关于php - Symfony 3 从现在开始找到最近的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37648303/

相关文章:

javascript - 如何使用 JQuery 和 Ajax 写入全局函数?

php - 保留使用 tcpdf 创建的 pdf 中的换行符和行间距

mysql - 智能搜索MySQL

php - 将数据从 1 个数组添加到另一个

Symfony RememberMe token_provider

php - 尽管在 header 中发送了 base64,但仍无法识别附件

sql - 如何从同一张表的父行中获取所有子项?

php - Symfony - 如何从 View 中选择的 html 标签获取数据

php - 多个连接无法识别选项 "mapping_types"

php - 用于 PHP/Android 的 Xliff 工具