在 Symfony API 中添加Where条件

问题描述 投票:0回答:1

我是 symfony 的新手,正在尝试学习 API。我有下面的 api 并且它工作正常

public function api_allAction(Request $request,$token)
    {
        if ($token!=$this->container->getParameter('token_app')) {
            throw new NotFoundHttpException("Page not found");  
        }
        $em=$this->getDoctrine()->getManager();

        $imagineCacheManager = $this->get('liip_imagine.cache.manager');
        $list=array();
        $categories =   $em->getRepository("AppBundle:Category")->findBy(array(),array("position"=>"asc"));
        foreach ($categories as $key => $category) {
            $s["id"]=$category->getId();
            $s["title"]=$category->getTitle();
            $s["image"]=$imagineCacheManager->getBrowserPath( $category->getMedia()->getLink(), 'category_thumb_api');
            $list[]=$s;
        }
        header('Content-Type: application/json'); 
        $encoders = array(new XmlEncoder(), new JsonEncoder());
        $normalizers = array(new ObjectNormalizer());
        $serializer = new Serializer($normalizers, $encoders);
        $jsonContent=$serializer->serialize($list, 'json');
        return new Response($jsonContent);
    }

我需要获得类别类型==0的结果,但我无法在其中添加条件。我对 symfony API 没有太多经验,所以无法提出这个条件。 谢谢!

php doctrine-orm
1个回答
0
投票

使用findBy()时,可以将第一个参数作为条件。

所以代替:

$categories = $em->getRepository("AppBundle:Category")->findBy(array(),array("position"=>"asc"));

试试这个:

$categories = $em->getRepository("AppBundle:Category")->findBy(array("category_type"=>0),array("position"=>"asc"));
© www.soinside.com 2019 - 2024. All rights reserved.