我在Doctrine中有2个实体:User和Club。两者都在权利中包含此文本:
/**
* @ORM\ManyToMany(targetEntity="User", mappedBy="sms_followers")
**/
private $smsfollowers;
另一个目标实体是Club。
我使用findOneBy()来检索俱乐部以获得概述。你可以看到俱乐部的所有数据。
现在我想知道有多少短信粉丝。我该怎么做?
答案 0 :(得分:0)
这应该让你开始运行。我将所有代码放在控制器中,以便您理解。
<?php
namespace Acme\YourBundle\Controller;
use Symfony\Component\DependencyInjection\ContainerAware;
class YourBundle extends ContainerAware
/**
* @return EntityManager
*/
protected function getEntityManager()
{
return $this->container
->get('doctrine')
->getEntityManager();
}
/**
* Get the number of followers for a particular class
*
* @param string The class
* @return int The number of followers
*/
protected function getNumberOfSMSFollowers($class)
{
$entityManager = $this->container
->get('doctrine')
->getEntityManager();
$queryBuilder = $entityManager
->createQueryBuilder()
->select('count(f.id)')
// from the appropriate class
->from($class, 'c')
->innerJoin('c.smsfollowers','f');
return $queryBuilder
->getQuery()
->getSingleScalarResult();
}
/**
* Get the number of followers in 2 different entities
*
* @return response The response you wish to give
*/
public function getNumberOfFollowersAction()
{
// Your classes
$userClass ="Acme\UserBundle\User";
$clubClass ="Acme\ClubBundle\Club";
$nbFollowersInUserClass = $this->getNumberOfSMSFollowers($userClass);
echo $nbFollowersInUserClass;
$nbFollowersInClubClass = $this->getNumberOfSMSFollowers($userClass);
echo $nbFollowersInClubClass;
//...
}
}