How to search entities ?

Help for integrating the Laravel package
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
MikaelNazarenko
Expert
Posts: 274
Joined: 27 Jun 2019, 16:19

How to search entities ?

Post by MikaelNazarenko » 17 Jul 2019, 10:27

My environment is the following:
Laravel Framework 5.8.26
aimeos-laravel 2019.04
PHP Version 7.2.19-1+ubuntu18.04.1+deb.sury.org+1

In the users_list table I have several rows with domain = media. In other words some user has several media attached to his.

My goal is to find all media of specific user and show it on admin tab. I created topic about it, but here I would like figure out another thing.

Code: Select all

/** @var \Aimeos\MShop\Common\Manager\Decorator\Depth $customerListsManager */
$customerListsManager = \Aimeos\MShop::create( $context, 'customer/lists' );

$search = $customerListsManager->createSearch();

$search->setConditions( $search->combine( '&&', [
         $search->compare( '==', 'customer.lists.domain', 'media' ),
         $search->compare( '==', 'customer.lists.parentid', Auth::id()),
] ) );

$items = $customerListsManager->searchItems($search, ['media'], $total);
dd($items);
The code above does what I need. It returns me customer/list items like this:

Code: Select all

  34 => Standard {#430 ▼
    -prefix: "customer.lists."
    -values: array:15 [▼
      "customer.lists.id" => "34"
      "customer.lists.siteid" => "1"
      "customer.lists.parentid" => "2"
      "customer.lists.type" => "default"
      "customer.lists.domain" => "media"
      "customer.lists.refid" => "96"
      "customer.lists.datestart" => null
      "customer.lists.dateend" => null
      "customer.lists.config" => []
      "customer.lists.position" => "0"
      "customer.lists.status" => "1"
      "customer.lists.mtime" => "2019-07-16 15:20:21"
      "customer.lists.editor" => "aimeos:account"
      "customer.lists.ctime" => "2019-07-16 15:20:21"
      "date" => "2019-07-17 10:06:00"
    ]
    -refItem: null
    -bdata: array:15 [▶]
    -available: true
    -modified: false
    -prefix: "customer.lists."
  }
Problem is there refItem is null! But actually I need this refItem, there as I know must be media object where I can get label and other. $items = $customerListsManager->searchItems($search, ['media'], $total); - here I have set media, so it must fetch media... Ideally would be if I can get exactly array of media objects.

So, such way I want to get all media from specific user. But also I want to make it work with filters in admin panel, so user can filter media by label and sort them.

Please provide correct code how to do it

Thank you a lot for the help!

User avatar
aimeos
Administrator
Posts: 7868
Joined: 01 Jan 1970, 00:00

Re: How to search entities ?

Post by aimeos » 17 Jul 2019, 10:32

Simply use this:

Code: Select all

$customer = \Aimeos\MShop::create( $context, 'customer' )->getItem( Auth::id(), 'media' );
foreach( $customer->getListItems( 'media', null, null, false ) as $listItem ) {
	if( ( $refItem = $listItem->getRefItem() ) !== null ) {
		// ...
	}
}
// or use this if you don't need the list items:
$mediaItems = $customer->getRefItems( 'media', null, null, false );
The second and third parameters are the type and list type of the returned items to filter for.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

MikaelNazarenko
Expert
Posts: 274
Joined: 27 Jun 2019, 16:19

Re: How to search entities ?

Post by MikaelNazarenko » 17 Jul 2019, 10:43

Ok, but please, take a look at the screenshot:

https://prnt.sc/og8258

This I meant about filters. I want to be able to filter and sort entities. As I understand your approach will look ugly if I want implement filtering and sorting. Because it will not create right sql query to filter entities at once. But I will need filter them in loop or using array functions.. Not good(

Do you understand what I mean ?

User avatar
aimeos
Administrator
Posts: 7868
Joined: 01 Jan 1970, 00:00

Re: How to search entities ?

Post by aimeos » 17 Jul 2019, 10:54

Because of the strict data domain model, you can't sort and filter items in foreign data domains. But the list items are sorted by their positions automatically if you use getItem().

If you implement your panel similar to the catalog/product panel, you can use filtering and sorting at least for the list items:
https://github.com/aimeos/ai-admin-jqad ... andard.php
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

MikaelNazarenko
Expert
Posts: 274
Joined: 27 Jun 2019, 16:19

Re: How to search entities ?

Post by MikaelNazarenko » 17 Jul 2019, 11:03

Do you mean this method ?

https://github.com/aimeos/ai-admin-jqad ... d.php#L288

Strange, why I can't get refItem from my first post in this topic

User avatar
aimeos
Administrator
Posts: 7868
Joined: 01 Jan 1970, 00:00

Re: How to search entities ?

Post by aimeos » 17 Jul 2019, 11:14

Yes, this is the method which retrieves the items but you also have to take a look at fromArray()/toArray().
The lists manager doesn't retrieve the referenced items. This is done in the domain manager (customer manager in your case).
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

MikaelNazarenko
Expert
Posts: 274
Joined: 27 Jun 2019, 16:19

Re: How to search entities ?

Post by MikaelNazarenko » 17 Jul 2019, 11:43

Hmm, honestly after using ActiveRecord and Repository pattern this approach is new for me and for now not very useful.

I have idea how to solve my problem. Firstly I get customer media list and then I get media ids. Then I can get exact media by their ids. Code looks like this:

Code: Select all

  /** @var \Aimeos\MShop\Common\Manager\Decorator\Depth $customerListsManager */
            $customerListsManager = \Aimeos\MShop::create( $context, 'customer/lists' );

            $search = $customerListsManager->createSearch();

            $search->setConditions( $search->combine( '&&', [
                $search->compare( '==', 'customer.lists.domain', 'media' ),
                $search->compare( '==', 'customer.lists.parentid', Auth::id()),
            ] ) );

            $items = $customerListsManager->searchItems($search, ['media'], $total);

            $mediaIds = [];
            foreach ($items as $item) {
                $mediaIds[] = $item->getRefId();
            }


            $search = $mediaManager->createSearch();
            $search->setConditions( $search->compare( 'in', 'media.id', $mediaIds ));
            $media = $customerListsManager->searchItems($search);
As for me it is ugly solution. But I don't know another way yet. And problem that I don't know how to search media with 'in' operator. I need get all media which IDS are in $mediaIds. How can I do it ?

User avatar
aimeos
Administrator
Posts: 7868
Joined: 01 Jan 1970, 00:00

Re: How to search entities ?

Post by aimeos » 17 Jul 2019, 11:55

Yes, your approach works, just use the "==" operator. It works for arrays too.

ActiveRecord and Respository patterns are unable to work with huge amounts of data efficiently. Thus we use DAO/DTO pattern to be able to manage and retrieve data in setups with billions of items in milliseconds.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

MikaelNazarenko
Expert
Posts: 274
Joined: 27 Jun 2019, 16:19

Re: How to search entities ?

Post by MikaelNazarenko » 17 Jul 2019, 16:07

Great thank you! But here I am also afraid for one thing.

The following code must collect all ids of media I need:

Code: Select all

            $items = $customerListsManager->searchItems($search);

            $mediaIds = [];
            foreach ($items as $item) {
                $mediaIds[] = $item->getRefId();
            }
But is there any limit for entities count ? Because if there is, for example, limit per 100 entities, but user has 200 entities - my logic will be broken

User avatar
aimeos
Administrator
Posts: 7868
Joined: 01 Jan 1970, 00:00

Re: How to search entities ?

Post by aimeos » 18 Jul 2019, 17:11

There's a default limit of 100 if you use createSearch() only. If you copy/paste/adapt the catalog/product sub-panel, there's a pagination included.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply