查询产品

p3rjfoxz  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(346)

我喜欢用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安装。
dpiehjr4

dpiehjr41#

因此,首先,要在akeneo中查询产品,应该使用产品查询生成器(productquerybuilder,pqb)。如果您使用的是1.6,下面是使用它的文档链接,非常简单:https://docs.akeneo.com/1.6/cookbook/catalog/product/query.html
要获得可用于pqb的属性和字段的过滤器的详尽列表,可以使用 php app/console pim:product:query-help 指挥你的个人信息管理系统。
正如您所注意到的,族不是一个属性而是一个字段,您可以在上面命令的字段过滤器中找到它:

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
}

相关问题