0

我喜欢使用 akeneo 中的自定义命令从数据库中获取一些产品。我正在使用ProductRepositoryInterface

public function read()
{
    return $this->repository->findBy(
        [
            'enabled' => true,
            'family' => ['projector', 'projector_child', 'projector_parent'],
        ]
    );
}

这是生成的查询:

SELECT t0.id AS id1, t0.is_enabled AS is_enabled2, t0.created AS created3, t0.updated AS updated4, t0.family_id AS family_id5 FROM pim_catalog_product t0 WHERE t0.is_enabled = ? AND t0.family_id IN (?)

正如您在声明中看到的那样,家庭作为 Id 串接在一起。但我想按家庭代码搜索。

我必须改变什么?其中Pim/Component/Catalog/Model/AbstractProduct是family 和familyId 的属性。所以必须有一种方法来查询家庭代码。

也许它是相关的,但这是一个 Akeneo 1.6 安装。

4

1 回答 1

1

因此,首先,要在 Akeneo 中查询产品,您应该使用Product Query Builder (PQB)。如果您使用的是 1.6,这里是使用它的文档的链接,它非常简单:https ://docs.akeneo.com/1.6/cookbook/catalog/product/query.html

要获得可与 PQB 一起使用的属性和字段过滤器的详尽列表,您可以php app/console pim:product:query-help在 PIM 上使用该命令。

正如您所注意到的,族不是属性而是字段,您可以在上面命令的字段过滤器中找到它:

php app/console pim:product:query-help

Useable field filters...
+-----------------+--------------------------------+-----------------------------------------------------------+
| field           | operators                      | filter_class                                              |
+-----------------+--------------------------------+-----------------------------------------------------------+
| family          | IN, NOT IN, EMPTY, NOT EMPTY   | Pim\Bundle\CatalogBundle\Doctrine\ORM\Filter\FamilyFilter |
| family.id       | IN, NOT IN, EMPTY, NOT EMPTY   | Pim\Bundle\CatalogBundle\Doctrine\ORM\Filter\FamilyFilter |
| family.code     | IN, NOT IN, EMPTY, NOT EMPTY   | Pim\Bundle\CatalogBundle\Doctrine\ORM\Filter\FamilyFilter |
+-----------------+--------------------------------+-----------------------------------------------------------+

您现在可以看到您可以在该family.code字段上进行搜索。

对于你的例子,你最终会得到这样的东西:

<?php
// Get a new instance of the PQB
$pqbFactory = $this->getContainer()->get('pim_catalog.query.product_query_builder_factory');
$pqb = $pqbFactory->create([
    'default_locale' => 'en_US',
    'default_scope' => 'ecommerce'
]);

// Now you can search for products with your family codes
$pqb->addFilter(
    'family.code',
    'IN',
    ['projector', 'projector_child', 'projector_parent']
);

// Retrieve your products
$productsCursor = $pqb->execute();
foreach ($productsCursor as $product) {
    // your custom logic
}
于 2018-08-13T08:42:31.417 回答