使用 FOSUserBundle 通过 queryBuilder 连接获取用户名
Get username with queryBuilder join using FOSUserBundle
我使用 FOSUserBundle,我需要获取在我的 post 上发表评论的用户名。
我使用函数 createQuery
获取 post 评论,并将此连接添加到尝试获取名称:
<?php
namespace CommentsBundle\Entity;
use FOS\UserBundle\Entity\User;
/**
* CommentsRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class CommentsRepository extends \Doctrine\ORM\EntityRepository
{
public function getPostComments($planId)
{
$arrPlanComments = array();
$query = $this->getEntityManager()->createQuery(
'SELECT
c.id,
c.fkUser,
c.fkReply,
c.comment,
c.createdAt,
c.likes,
c.unlikes
FROM CommentsBundle:Comments c
INNER JOIN UserBundle:User u
WITH (c.fkUser = u.id)
WHERE
c.fkPlan = :planId
ORDER BY c.createdAt DESC'
)->setParameter('planId', $planId);
try
{
$arrPlanComments = $query->getArrayResult();
}
catch(\Doctrine\ORM\NoResultException $ex)
{
echo $ex->getMessage();
}
return $arrPlanComments;
}
}
我已将 FOSUserBundle 扩展到名为 'UserBundle' 的自定义包,它工作正常,但我不知道如何添加与该实体的关系。
我在添加连接关系时遇到此错误:
[Semantical Error] line 0, col 164 near 'UserBundle:User': Error: Class 'UserBundle\Entity\User' is not defined.
怎么了?
根据文档,您必须 create own class 用户实体,这将扩展 FOSUser 提供的实体。好像你还没有那样做。这就是您的错误消息可能表明的内容。
我使用 FOSUserBundle,我需要获取在我的 post 上发表评论的用户名。
我使用函数 createQuery
获取 post 评论,并将此连接添加到尝试获取名称:
<?php
namespace CommentsBundle\Entity;
use FOS\UserBundle\Entity\User;
/**
* CommentsRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class CommentsRepository extends \Doctrine\ORM\EntityRepository
{
public function getPostComments($planId)
{
$arrPlanComments = array();
$query = $this->getEntityManager()->createQuery(
'SELECT
c.id,
c.fkUser,
c.fkReply,
c.comment,
c.createdAt,
c.likes,
c.unlikes
FROM CommentsBundle:Comments c
INNER JOIN UserBundle:User u
WITH (c.fkUser = u.id)
WHERE
c.fkPlan = :planId
ORDER BY c.createdAt DESC'
)->setParameter('planId', $planId);
try
{
$arrPlanComments = $query->getArrayResult();
}
catch(\Doctrine\ORM\NoResultException $ex)
{
echo $ex->getMessage();
}
return $arrPlanComments;
}
}
我已将 FOSUserBundle 扩展到名为 'UserBundle' 的自定义包,它工作正常,但我不知道如何添加与该实体的关系。
我在添加连接关系时遇到此错误:
[Semantical Error] line 0, col 164 near 'UserBundle:User': Error: Class 'UserBundle\Entity\User' is not defined.
怎么了?
根据文档,您必须 create own class 用户实体,这将扩展 FOSUser 提供的实体。好像你还没有那样做。这就是您的错误消息可能表明的内容。