Php 在Symfony2中使用docrtrine

Php 在Symfony2中使用docrtrine,php,symfony,doctrine,Php,Symfony,Doctrine,我正在从事一个php项目,但数据库有问题,我使用以下代码从数据库获取数据: public function getSeenAction(Request $request , $notificationId) { $sessionId = $request->headers->get('SessionID'); if( $sessionId == null ) { //return new Response("Unauthorized",401)

我正在从事一个php项目,但数据库有问题,我使用以下代码从数据库获取数据:

public function getSeenAction(Request $request , $notificationId)
{
    $sessionId = $request->headers->get('SessionID');
    if( $sessionId == null )
    {
        //return new Response("Unauthorized",401);
    }
    $notificationRepo = $this->getDoctrine()->getRepository('MegasoftEntangleBundle:Notification');
    $notification = $notificationRepo->findOneById($notificationId);

    if($notification == null)
    {
        return new Response("Notification not found" ,404);
    }

    $seen = $notification->getSeen();
    $response = new JsonResponse();
    $response->setdata(array('seen'=>$seen));
    $response->setStatusCode(200);
    return $response;
}
我在其他表中尝试了相同的代码,结果成功了,但是每当我从
通知
表中检索数据时,它总是给出null,尽管该表包含数据

$notificationRepo = $this->getDoctrine()->getRepository('MegasoftEntangleBundle:Notification');
$notification = $notificationRepo->findAll();
var_dump(notification);

这段代码是否返回了一些信息?可能您的
NotificationRepository.php
的代码不好,您能把它放上去吗?

如果您只想按Id查找记录,请尝试使用
查找
而不是
查找Id

另一方面,如果要使用
findOneBy
,则传递的条件参数应为数组

$result = $notificationRepo->find($notificationId);


确保在NotificationRepository.php文件中为
findOneById
提供了正确的代码

那你可以查一下

if (!empty($result)) { ... }
if (!empty($result)) { ... }