How in Yii2 ActiveDataProvider to separate multiple criteria?



  • I need to ActiveDataProvider Records particular to a criterion (e.g. alphabet) first at a positive price of the goods and then at the same criterion, but at a zero price of the goods. Trying to write two. ActiveDataProviderand then connect them to one, but it doesn't work, and it's wrong, apparently.

    $notAvailableDataProvider = new ActiveDataProvider($dataProvider);
    $notAvailableDataProvider->query->andWhere('`price` = ' . '0');
    $dataProvider = new ActiveDataProvider($dataProvider, $notAvailableDataProvider);
    

    Can you tell me how to do this? It's important to finally get it. ActiveDataProvider



  • I see a few options.

    First:

    ActiveDataProvider Provides data, getting them in response to the request you're configured. The request shall be subject to the type of yii\db\QueryInterface♪ You have to configurate your own. ActiveDataProvider such a copy that would have obtained the necessary data already classified, this could be done using UNIONSql The request shall be given a plus minus:

    (SELECT * FROM products WHERE price > 0 ORDER BY title ASC)
    UNION
    (SELECT * FROM products WHERE price = 0 ORDER BY title ASC);
    

    That's what it means. UNIONyou combine the withdrawal of two sampling requests. In your first request, you choose goods with a positive price, second with zero. In two requests, the same classification criterion is specified.

    Again:

    • create a request (e.g., ActiveQuery or Query)
    • using the facility ' s interface, describe sql request containing the expression UNIONwhich brings together the two samples you need.
    • Configuration of the subject ActiveDataProvider and work with him.

    This approach has its flaws, but it's very likely that you'll be able to complete it.

    Second:

    Let's get rid of the idea that

    It is important to eventually obtain the ActiveDataProvider

    We think otherwise: Ultimately, an object that operates an interface is needed. yii\data\DataProviderInterface

    This is a more appropriate, objective-oriented approach: Your application modules should not depend on specific realities, but should rely on interfaces.

    Next, we'll create a class that's empirical. yii\data\DataProviderInterface

    We're describing his designer so he can take some type of copies at the inlet. yii\data\DataProviderInterfacethat we keep in the class field. Implement the methods described in the interface by deposing them alternately to each of the copies delivered to the designer yii\data\DataProviderInterfaceand in the classroom we collect their results, process and return the client.

    On the clint, we create two objects. ActiveDataProvidereach of which is configured by one individual sql The request (the first receives goods with a positive price, the second with zero), collects them. Create the object of the class we wrote, transferring it to the designer of this collection. We're working with a copy of our class as we'd work with. ActiveDataProvider

    The second option is more difficult, but it offers much more flexibility to expand the application future. The first option is available some stunning chardcod - You say with your hands, the general criterion of grading, changing it in the run would be difficult. In the second, you can configure the collection elements with a common object. yii\data\Sort, may be configured differently, taking decisions on the implementation of the programme, taking into account the parameters of the request or the context of the status of the annex. On a larger account, the first option can also be developed to the possibility of a configuration in the ranks, but it will be less elegant at best, at the worst, half with crutches. But in general, I think that it's the first option that you might be able to come.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2