Php QueryBuilder每月创建的用户数-Symfony 3.4
嘿,伙计们,我正在尝试做统计,我想做一个图表,每个月创建的帐户数,但我不知道如何做 目前,我的项目如下所示:Php QueryBuilder每月创建的用户数-Symfony 3.4,php,sql,symfony,statistics,query-builder,Php,Sql,Symfony,Statistics,Query Builder,嘿,伙计们,我正在尝试做统计,我想做一个图表,每个月创建的帐户数,但我不知道如何做 目前,我的项目如下所示: ProfileRepo : namespace AppBundle\Repository; use Doctrine\ORM\EntityRepository; class ProfileRepository extends EntityRepository { function countNumberUsersPerMonth($year, $month) {
ProfileRepo :
namespace AppBundle\Repository;
use Doctrine\ORM\EntityRepository;
class ProfileRepository extends EntityRepository
{
function countNumberUsersPerMonth($year, $month) {
$query = $this->createQueryBuilder('p')
->Where('YEAR(p.created_at) = ?', $year)
->andWhere('MONTH(p.created_at) = ?', $month)
->getQuery();
return $query->getOneOrNullResult();
}
}
我的控制器(此处用户月很重要):
配置文件实体:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Profile
*
* @ORM\Table(name="profile", uniqueConstraints=
{@ORM\UniqueConstraint(name="profile_id_uindex", columns={"id"})})
* @ORM\Entity(repositoryClass="AppBundle\Repository\ProfileRepository")
*/
class Profile implements \JsonSerializable
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
* @ORM\OneToOne(targetEntity="User", inversedBy="Profile")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="resume", type="text", length=65535, nullable=true)
*/
protected $resume;
/**
* @var string
*
* @Assert\Image()
* @ORM\Column(name="avatar_path", type="string", length=255, nullable=true)
*/
protected $avatarPath;
/**
* @var \DateTime
*
* @ORM\Column(name="last_connexion", type="datetime", nullable=false)
*/
protected $lastConnexion;
/**
* @var \DateTime
*
* @ORM\Column(name="birth", type="datetime", nullable=false)
*/
protected $birth;
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=255, nullable=false, unique=true)
*/
protected $email;
/**
* @var integer
*
* @ORM\Column(name="level", type="integer", nullable=false)
*/
protected $level = '1';
/**
* @var string
*
* @ORM\Column(name="phone", type="string", length=60, nullable=true, unique=true)
* @Assert\Regex(
* pattern="/(0|\+)[1-9]([-. ]?[0-9]{2}){4}/",
* message="You need to put a french number ! Starting with 0 or +33 !",
* )
*/
protected $phone;
/**
* @var string
*
* @ORM\Column(name="language", type="text", length=65535, nullable=true)
*/
protected $language;
/**
* @var boolean
*
* @ORM\Column(name="is_male", type="boolean", nullable=false)
*/
protected $isMale = '1';
/**
* @var \DateTime
*
* @ORM\Column(name="created_account", type="datetime", nullable=false)
*/
protected $createdAccount = 'CURRENT_TIMESTAMP';
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @param int $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return string
*/
public function getResume()
{
return $this->resume;
}
/**
* @param string $resume
*/
public function setResume($resume)
{
$this->resume = $resume;
}
/**
* @return string
*/
public function getAvatarPath()
{
return $this->avatarPath;
}
/**
* @param string|null
*/
public function setAvatarPath($avatarPath)
{
$this->avatarPath = $avatarPath;
}
/**
* @return \DateTime
*/
public function getLastConnexion()
{
return $this->lastConnexion;
}
/**
* @param \DateTime $lastConnexion
*/
public function setLastConnexion(\DateTime $lastConnexion)
{
$this->lastConnexion = $lastConnexion;
}
/**
* @return \DateTime
*/
public function getBirth()
{
return $this->birth;
}
/**
* @param \DateTime $birth
*/
public function setBirth(\DateTime $birth)
{
$this->birth = $birth;
}
/**
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* @param string $email
*/
public function setEmail($email)
{
$this->email = $email;
}
/**
* @return int
*/
public function getLevel()
{
return $this->level;
}
/**
* @param int $level
*/
public function setLevel($level)
{
$this->level = $level;
}
/**
* @return string
*/
public function getPhone()
{
return $this->phone;
}
/**
* @param string|null
*/
public function setPhone($phone)
{
$this->phone = $phone;
}
/**
* @return string
*/
public function getLanguage()
{
return $this->language;
}
/**
* @param string $language
*/
public function setLanguage($language)
{
$this->language = $language;
}
/**
* @return \DateTime
*/
public function getCreatedAccount()
{
return $this->createdAccount;
}
/**
* @param \DateTime $createdAccount
*/
public function setCreatedAccount(\DateTime $createdAccount)
{
$this->createdAccount = $createdAccount;
}
/**
* @return bool
*/
public function isMale()
{
return $this->isMale;
}
/**
* @param bool $isMale
*/
public function setIsMale($isMale)
{
$this->isMale = $isMale;
}
function jsonSerialize()
{
return array(
"id" => $this->id,
"male" => $this->isMale,
"language" => $this->language,
"level" => $this->level,
"email" => $this->email,
"resume" => $this->resume,
);
}
}
但我很确定这不是正确的方法
看起来像:
好极了,有人可以帮thx:)首先,改变这个:
/**
* @var \DateTime
*
* @ORM\Column(name="last_connexion", type="datetime", nullable=false)
*/
protected $lastConnexion;
/**
* @var \DateTime
*
* @ORM\Column(name="created_account", type="datetime", nullable=false)
*/
protected $createdAccount = 'CURRENT_TIMESTAMP';
为此:
//You don't need to write nullable=false, it's the default option.
/**
* @var \DateTime
*
* @ORM\Column(name="last_connexion", type="datetime")
*/
protected $lastConnexion;
/**
* @var \DateTime
*
* @ORM\Column(name="created_account", type="datetime")
*/
protected $createdAccount;
//Wil set/update timestamps on pre-persist/pre-update
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function updatedTimestamps() {
$this->lastConnexion(new \DateTime());
//Will occur only once, on pre-persists.
if($this->createdAccount() == null) {
$this->setCreation(new \DateTime());
}
}
要获取每月创建的用户数,您需要使用基本SQL查询:
src/AppBundle/Repository/ProfileRepository.php
结果应该是这样的:
array:2 [▼
0 => array:2 [▼
"month" => "2018-6"
"total" => "47"
]
1 => array:2 [▼
"month" => "2018-7"
"total" => "13"
]
]
剩下要做的就是用普通PHP处理结果。。。
应该很容易…;) 最后,你具体需要哪方面的帮助?这个问题?Javascript?解析数据?你的问题太模糊了。。。事实上,这不是一个真正的问题……我需要查询方面的帮助,我不知道如何返回一个包含每月用户数的数组,就像我想返回的[233023938283828]我不知道这是否可能,但每个月需要一段时间
/**
* @var \DateTime
*
* @ORM\Column(name="last_connexion", type="datetime", nullable=false)
*/
protected $lastConnexion;
/**
* @var \DateTime
*
* @ORM\Column(name="created_account", type="datetime", nullable=false)
*/
protected $createdAccount = 'CURRENT_TIMESTAMP';
//You don't need to write nullable=false, it's the default option.
/**
* @var \DateTime
*
* @ORM\Column(name="last_connexion", type="datetime")
*/
protected $lastConnexion;
/**
* @var \DateTime
*
* @ORM\Column(name="created_account", type="datetime")
*/
protected $createdAccount;
//Wil set/update timestamps on pre-persist/pre-update
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function updatedTimestamps() {
$this->lastConnexion(new \DateTime());
//Will occur only once, on pre-persists.
if($this->createdAccount() == null) {
$this->setCreation(new \DateTime());
}
}
class ProfileRepository extends \Doctrine\ORM\EntityRepository {
public function countByMonth() {
try {
$stmt=$this->getEntityManager()
->getConnection()
->prepare("SELECT CONCAT(YEAR(`p`.`created_account`), '-', MONTH(`p`.`created_account`)) AS `month`, COUNT(`p`.`id`) AS `total` FROM `profile` AS `p` GROUP BY YEAR(`p`.`created_account`), MONTH(`p`.`created_account`)");
$stmt->execute();
return $stmt->fetchAll();
} catch(DBALException $e) {
return $e;
}
}
}
array:2 [▼
0 => array:2 [▼
"month" => "2018-6"
"total" => "47"
]
1 => array:2 [▼
"month" => "2018-7"
"total" => "13"
]
]