Query products with Doctrine ind Akeneo

2019-09-02 06:10发布

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'],
        ]
    );
}

And this is the generated query:

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 (?)

As you can see in the Statement, the family is threaded as an Id. But I want to search by the family code.

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.

Maybe it's relevant, but this is an Akeneo 1.6 installation.

1条回答
三岁会撩人
2楼-- · 2019-09-02 06:42

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

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 |
+-----------------+--------------------------------+-----------------------------------------------------------+

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
}
查看更多
登录 后发表回答