Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added QueryInterface::emulateExecution() #12708

Closed
wants to merge 4 commits into from

Conversation

klimov-paul
Copy link
Member

@klimov-paul klimov-paul commented Oct 10, 2016

Q A
Is bugfix? no
New feature? yes
Breaks BC? yes
Tests pass? yes
Fixed issues #6373, #12390

Added QueryInterface::emulateExecution(), which allows preventing of the actual query execution.
This allows to cancel DataProvider preventing search query execution in case search model is invalid:

public function search($params)
    {
        $query = Item::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            $query->where('0=1');
            $query->emulateExecution(); // No SQL execution will be done
            return $dataProvider;
        }

This also fix unecessary query in case of via() usage. See #12390.

Note: this PR produces minor BC break as it alters yii\db\QueryInterface, however the implementation of new method is present at yii\db\QueryTrait - thus it is less likely to produce a trouble.

@klimov-paul klimov-paul added status:under discussion type:enhancement feature:db severity:BC breaking Either breaks backwards compatibility or fixed previously made breakage labels Oct 10, 2016
@klimov-paul klimov-paul added this to the 2.0.11 milestone Oct 10, 2016
* @return $this the query object itself
* @since 2.0.11
*/
public function cancel();
Copy link
Member

@samdark samdark Oct 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the name of the method isn't OK since we as users do not clearly separate actual SQL query and getting the result. I propose using another name such as skipQuery() (not sure it's the best one).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to preventExecution()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another possibility is 'simulate'

@klimov-paul klimov-paul changed the title Added QueryInterface::cancel() Added QueryInterface::preventExecution() Oct 12, 2016
@samdark
Copy link
Member

samdark commented Oct 12, 2016

Still looks like a hacky solution but I can't suggest anything better...

@klimov-paul klimov-paul changed the title Added QueryInterface::preventExecution() Added QueryInterface::emulateExecution() Oct 12, 2016
@samdark
Copy link
Member

samdark commented Oct 12, 2016

Oh! Now I like the naming.

@dynasource
Copy link
Member

emulate is even better

@dynasource
Copy link
Member

I've reviewed it. I don't know with this solution. It works but it seems to be an overkill for realizing the objective 'DataProvider preventing search query execution'. Aren't there any other solutions out there that are less obtrusive?

Copy link
Member

@cebe cebe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good changes! Need a CHANGELOG and UPGRADE note. Will add them myself. Merging now.

@@ -1312,4 +1312,69 @@ public function testAttributeAccess()
$baseModel = new ActiveRecord();
$this->assertFalse($baseModel->hasProperty('unExistingColumn'));
}

public function testEmulateExecution()
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could assert here that the customer table is not empty, otherwise the test would pass without emulateExecution() to work

@klimov-paul klimov-paul deleted the 6373-query-cancel branch January 16, 2017 11:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature:db severity:BC breaking Either breaks backwards compatibility or fixed previously made breakage status:under discussion type:enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants