Symfony2 ElasticSearch排序映射错误

时间:2016-01-28 08:22:53

标签: php symfony elasticsearch

我正在尝试使用弹性按价格对产品进行排序。但由于某种原因,该字段未映射,我收到错误。我不知道我在做什么,这是我第一次使用这样的东西..帮助应该很棒!

在我的产品实体中,我有一个名为base的简单字段,用于存储价格:

/**
 * @ORM\Column(name="base", type="decimal", precision=10, scale=2, nullable=true)
 */
private $base;

我的config.yml:

fos_elastica:
    default_manager: orm
    clients:
        default: { host: localhost, port: 9200 }
    indexes:
        app:
            index_name: app_%kernel.environment%
            types:
                product:
                    mappings:
                        id:
                            type: integer
                        adminTitle:
                            type: string
                        base:
                            type: double
                    persistence:
                        # the driver can be orm, mongodb, phpcr or propel
                        # listener and finder are not supported by
                        # propel and should be removed
                        driver: orm
                        model: Mp\ShopBundle\Entity\Product
                        provider: ~
                        listener:
                            immediate: ~
                        finder: ~
                        repository: Mp\ShopBundle\Repository\ProductRepository
                        elastica_to_model_transformer:
                            query_builder_method: findProductsBySearch
                            ignore_missing: true

所以为了获得我正在使用我自己的功能的产品:

    public function findProductsBySearch($data)
    {

        $qb = $this->createQueryBuilder('o');
        $qb->addSelect('trans')->join('o.translations', 'trans');
        $qb->addSelect('gallery')->join('o.gallery', 'gallery');
        $qb->addSelect('media')->join('gallery.galleryHasMedias', 'media');
        $qb->addSelect('image')->join('media.media', 'image');
        $qb->where("LOWER(o.adminTitle) LIKE :name");
        $qb->SetParameter('name', '%'.strtolower($data).'%');
//
//        var_dump($qb);
//        die;

        return $qb;
//        return $qb->getQuery();

    }

问题:

当我尝试对找到的产品进行排序时出现错误:

Parse Failure [No mapping found for [o.base] in order to sort on]

如果我的查询,我添加:

$qb->addSelect('o.base');

我明白了:

Cannot read property "id" from an array. Maybe you intended to write the property path as "[id]" instead.

现在我陷入困境......

P.S

我在我的枝条中使用knp paginator进行排序:

<ul class="dropdown-menu">
   <li>{{  knp_pagination_sortable(pagination, 'Más barato', 'o.base', {'direction': 'desc'}) }}</li>
   <li>{{ knp_pagination_sortable(pagination, 'Más caro', 'o.base', {'direction': 'asc'}) }}</li>
   <li>{{ knp_pagination_sortable(pagination, 'Más nuevo', 'o.created_at', {'direction': 'desc'}) }}</li>
   <li>{{ knp_pagination_sortable(pagination, 'Más antiguo', 'o.created_at', {'direction': 'asc'}) }}</li>
</ul>

0 个答案:

没有答案