dql select返回空结果symfony 3

时间:2017-12-07 20:23:41

标签: php symfony dql empty-list symfony3.x

我正在尝试从包含关键字的数据库中获取所有视频,这是使用Symfony 3的API REST构建。以下是URL的示例

http://localhost/Server/symfony/web/app_dev.php/video/search/prueba

“prueba”是我试图找到的关键字

这是函数..

public function searchAction(Request $request, $search = null){
    $helper = $this->get("app.helper");
    $em = $this->getDoctrine()->getManager();
    if($search != null){
        $dql = "SELECT v.title FROM BackendBundle:Video v "
                . "WHERE v.title LIKE :search OR "
                . "v.description LIKE :search ORDER BY v.id DESC";

        $query = $em->createQuery($dql)
                ->setParameter("search", "%search%");
    } else {
        $dql = "SELECT v FROM BackendBundle:Video v ORDER BY v.id DESC";
        $query = $em->createQuery($dql);
    }
    $page = $request->query->getInt("page", 1);
    $paginator = $this->get("knp_paginator");
    $items_per_page = 6;
    $pagination = $paginator->paginate($query, $page, $items_per_page);

    $total_items_count = $pagination->getTotalItemCount();
    $data = array(
        "status" => "success",
        "total_items_count" => $total_items_count,
        "page_actual" => $page,
        "items_per_page" => $items_per_page,
        "total_pages" => ceil($total_items_count/$items_per_page),
        "data" => $pagination
    );
    return $helper->toJson($data);
}

结果应该是一个包含4个视频的JSON ..但是我得到了

{
    "status": "success",
    "total_items_count": 0,
    "page_actual": 1,
    "items_per_page": 6,
    "total_pages": 0,
    "data": []
}

其中“data”是包含视频数据的数组

有人知道我想念的是什么吗? dql请教..这是什么?

有人可以告诉我这是什么错误吗?我需要一个线索..dql咨询是正确的吗?

2 个答案:

答案 0 :(得分:0)

我看到您使用knp_paginator服务。尝试从像这样的paginator获取项目:

$data = array(
    "status" => "success",
    "total_items_count" => $total_items_count,
    "page_actual" => $page,
    "items_per_page" => $items_per_page,
    "total_pages" => ceil($total_items_count/$items_per_page),
    "data" => $pagination->getItems()
);

答案 1 :(得分:0)

感谢您的重播人!

所以..我在输入时犯了一个错误..我在搜索词之前忘记了$ ..

那是问题

    $query = $em->createQuery($dql)
            ->setParameter("search", "%search%"); <-- here 

非常感谢!!