问题描述
我喜欢使用akeneo中的自定义命令从数据库中获取一些产品。
我正在使用 ProductRepositoryInterface
I like to fetch some products from the Database with a custom command in akeneo.I'm using the 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。但是我想按家庭代码进行搜索。
As you can see in the Statement, the family is threaded as an Id. But I want to search by the family code.
我需要更改什么?在 Pim / Component / Catalog / Model / AbstractProduct
中,
是family和familyId的属性。因此,必须有一种查询家庭代码的方法。
What I have to change?In the Pim/Component/Catalog/Model/AbstractProduct
is an attribute for the family and familyId. So there have to be a way to query for the family code.
也许是相关的,但这是Akeneo 1.6安装。
Maybe it's relevant, but this is an Akeneo 1.6 installation.
推荐答案
因此,首先,要在Akeneo中查询产品,应使用产品查询生成器(PQB)。如果您使用的是1.6,则以下是使用它的文档的链接,它非常简单:
So first, to query products in Akeneo, you should use the Product Query Builder (PQB). If you're using the 1.6, here is the link to the documentation to use it, it's pretty straightforward: https://docs.akeneo.com/1.6/cookbook/catalog/product/query.html
属性&的过滤器的详尽列表可以与PQB一起使用的字段,则可以在PIM上使用 php应用程序/控制台pim:product:query-help
命令。
To have an exhaustive list of the filters on attributes & fields that can be used with the PQB, you can use the php app/console pim:product:query-help
command on your PIM.
您注意到,家庭不是属性而是字段,您会在上面命令的字段过滤器中找到它:
As you noticed, the family is not an attribute but a field, you'll find it in the field filters of the command above:
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
字段中进行搜索。
You can see now that you can search on the family.code
field.
对于您的示例,最终会得到类似
For your example, you'll end up with something like this:
<?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
}
这篇关于使用Doctrine ind Akeneo查询产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!