How to limit object loading to objects having an attribute in a distinct way?

Hi together,

I am new to pimcore and also to symphony. I tried to search for the answer to my question, but was not able to find it. I have a Class generated by Pimcore and I want to retrieve all objects of this class, but only one object per attribute value (distinct).
The equivalent SQL query would be something like: “SELECT DISTTINCT Attribute FROM Class”.
What is the best way to achieve this?

Many thanks in advance.

No answers so far. Let me try another way: How to execute SQL statements from within Pimcore code with the least effort. Maybe, there´s a easy way to convert that result into objects?

hmm that´s sad. Seems this is a unusual use case…

Hello!

It actually is an unusual use case. That being said, you can use the distinct keyword in your queries.

Since you’re new to Pimcore and Symfony, I suggest you read up on the Listings of Pimcore described here: https://pimcore.com/docs/5.x/Development_Documentation/Objects/Working_with_PHP_API.html#page_Object-Listings

The question is, what type of an attribute is it? Different attributes have different implementations. If I understood correctly, you’d like to, for example, take all values of all attributes in all objects of a single class for, say, filtering purposes or something like that?

If that is the case, then you also want to look at this: https://pimcore.com/docs/5.x/Development_Documentation/E-Commerce_Framework/Filter_Service/index.html

Be aware, though, that the Filter Service is only available if you have the E-Commerce Framework installed.

In any case, I would recommend to heavily use the PHP API. The less SQL statements, the better, since there’s always a chance of doing irreparable damage to your database. And the database model is actually more convoluted than you might think.

To achieve something like that through PHP, though, the chunk of code would be this:

$listing = new DataObject/Class/Listing();

$attributeArray = array_unique(
    array_map(
        function($classObject){
            $classObject->getAttribute();
        },
        $listing->getObjects()
    )
);

That’s, of course, assuming, that your class is actually named Class, and the attribute you’re trying to get is named Attribute, and that it returns a single value (is text, or a relation to an object, or something like that). If it’s a selection, then you have different options (the class definition itself has all the options saved already).

Hope this helped out a bit.

Thanks for your elaborate answer. That worked for me :slight_smile: