本文介绍了错误:无效的PathExpression。必须是StateFieldPathExpression。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用查询生成器构建 symfony项目实体

I'm working on a symfony project entity with query builder. When I try to run this function I get this issue.



public function json_filterAllproductsAction() {

    $search = "";
    $category = 1;

    //Combine tables and create the query with querybuilder
    $em = $this->container->get('doctrine.orm.entity_manager');

    $qb = $em->createQueryBuilder();

    $qb->select('p.category')
            ->from('EagleAdminBundle:Products', 'p')
            ->orderBy('p.id', 'DESC');
    if ($category != 0) {
        $qb->andWhere('p.category = :category')
                ->setParameter('category', $category);
    }
    $qb->andWhere('p.productTitle LIKE :title')
            ->setParameter('title', "$search%");

    //convert to json using "JMSSerializerBundle"
    $serializer = $this->container->get('serializer');
    $jsonproducts = $serializer->serialize($qb->getQuery()->getResult(), 'json');
    return new Response($jsonproducts);
}

我认为错误在于,

很大的帮助,有人可以帮助我。

It would be great help someone can help me.

推荐答案

您还需要在联接中获取类别。这样的事情应该可以正常工作:

You need to fetch category as well in your join. Something like this should work fine:

    $qb->select('p', 'c')
        ->from('EagleAdminBundle:Products', 'p')
        ->orderBy('p.id', 'DESC')
        ->join('p.category', 'c');

    if ($category != 0) {

        $qb->andWhere('p.category = :category')
            ->setParameter('category', $category);
    }

    $qb->andWhere('p.productTitle LIKE :title')
        ->setParameter('title', "$search%");

请注意,如果您不想将搜索限制为仅具有类别的产品,则可以更改

Note if you don't want to limit your search to only products that have categories you can change the join to a leftJoin.

还请注意,您可以将序列化器配置为序列化产品的category属性。然后,您应该只能够获取产品,并让它自动为您序列化类别。

Also note you can have the serializer configured to serialize the category property of product. Then you should just be able to fetch a product and have it automatically serialize the category for you.

这篇关于错误:无效的PathExpression。必须是StateFieldPathExpression。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 10:19