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

Default scope missing #1469

Closed
twinni opened this Issue Dec 9, 2013 · 22 comments

Comments

Projects
None yet
@twinni

twinni commented Dec 9, 2013

Yii1 has a function called defaultScope().
It seems that it's dropped in Yii2?
What is your suggestion to substitute this function?

Is there an other way (next to defaultScope) to define a default sort sequence for ActiveRecord's?

@slavcodev

This comment has been minimized.

Show comment
Hide comment
@slavcodev

slavcodev Dec 9, 2013

Contributor

Yes in Yii2 is dropped, because this method make many problems.
You can simple add default scope, e.q

class Post extends ActiveRecord
{
        public static function createQuery()
        {
               return parent::createQuery()->defaultWhere();
        }
        public static function defaultOrder($query)
        {
              $query->order('id desc');
        }
}
Contributor

slavcodev commented Dec 9, 2013

Yes in Yii2 is dropped, because this method make many problems.
You can simple add default scope, e.q

class Post extends ActiveRecord
{
        public static function createQuery()
        {
               return parent::createQuery()->defaultWhere();
        }
        public static function defaultOrder($query)
        {
              $query->order('id desc');
        }
}
@qiangxue

This comment has been minimized.

Show comment
Hide comment
@qiangxue

qiangxue Dec 9, 2013

Member

As, this is dropped in 2.0 as @slavcodev explained.

Member

qiangxue commented Dec 9, 2013

As, this is dropped in 2.0 as @slavcodev explained.

@qiangxue qiangxue closed this Dec 9, 2013

@twinni

This comment has been minimized.

Show comment
Hide comment
@twinni

twinni Dec 9, 2013

Thanks,

this way it looks and works very well.
But the order clause is named 'orderBy'. So the example should look like

class Post extends ActiveRecord
{
        public static function find($q = null)
        {
                return parent::find()->defaultOrder();
        }
        public static function defaultOrder($query)
        {
              $query->orderBy('id desc');
        }
}

twinni commented Dec 9, 2013

Thanks,

this way it looks and works very well.
But the order clause is named 'orderBy'. So the example should look like

class Post extends ActiveRecord
{
        public static function find($q = null)
        {
                return parent::find()->defaultOrder();
        }
        public static function defaultOrder($query)
        {
              $query->orderBy('id desc');
        }
}
@makroxyz

This comment has been minimized.

Show comment
Hide comment
@makroxyz

makroxyz Dec 12, 2013

Contributor

Probably it is a silly question... but

class Post extends ActiveRecord
{
    public static function find($q = null)
    {
        return parent::find()->defaultWhere();
    }

    public static function defaultWhere($query)
    {
        $query->andWhere(['published' => 1]);
     }
}

in this way Post::find(1) will not work... Am I wrong?

Contributor

makroxyz commented Dec 12, 2013

Probably it is a silly question... but

class Post extends ActiveRecord
{
    public static function find($q = null)
    {
        return parent::find()->defaultWhere();
    }

    public static function defaultWhere($query)
    {
        $query->andWhere(['published' => 1]);
     }
}

in this way Post::find(1) will not work... Am I wrong?

@samdark

This comment has been minimized.

Show comment
Hide comment
@samdark

samdark Dec 12, 2013

Member

You forgot return $this in your defaultWhere.

Member

samdark commented Dec 12, 2013

You forgot return $this in your defaultWhere.

@cebe

This comment has been minimized.

Show comment
Hide comment
@cebe

cebe Dec 12, 2013

Member

Nope, the scope should not return anything, it also is a static method and can not return $this.
In your code you just forgot to pass parameter $q, change it to this:

    public static function find($q = null)
    {
        return parent::find($q)->defaultWhere();
    }
Member

cebe commented Dec 12, 2013

Nope, the scope should not return anything, it also is a static method and can not return $this.
In your code you just forgot to pass parameter $q, change it to this:

    public static function find($q = null)
    {
        return parent::find($q)->defaultWhere();
    }
@slavcodev

This comment has been minimized.

Show comment
Hide comment
@slavcodev

slavcodev Dec 12, 2013

Contributor

Nope :) Correct is

class Post extends ActiveRecord
{
    public static function createQuery()
    {
        return parent::createQuery()->defaultWhere();
    }

    public static function defaultWhere($query)
    {
        $query->andWhere(['published' => 1]);
     }
}

becouse find method with params return e record instance not query.

Contributor

slavcodev commented Dec 12, 2013

Nope :) Correct is

class Post extends ActiveRecord
{
    public static function createQuery()
    {
        return parent::createQuery()->defaultWhere();
    }

    public static function defaultWhere($query)
    {
        $query->andWhere(['published' => 1]);
     }
}

becouse find method with params return e record instance not query.

@cebe

This comment has been minimized.

Show comment
Hide comment
@cebe

cebe Dec 12, 2013

Member

Yeah, right :) Thanks for pointing that out!

Also note that this default where will get overridden by calls to where(). Only uses of orWhere() and andWhere() will keep it.

Member

cebe commented Dec 12, 2013

Yeah, right :) Thanks for pointing that out!

Also note that this default where will get overridden by calls to where(). Only uses of orWhere() and andWhere() will keep it.

@makroxyz

This comment has been minimized.

Show comment
Hide comment
@makroxyz

makroxyz Dec 12, 2013

Contributor

@slavcodev

I've already tried you solution... suppose Post #1 is not published
Post::find(1) returns the post #1 discarding defaultWhere condition because find($q) method overwrites the where condition

Contributor

makroxyz commented Dec 12, 2013

@slavcodev

I've already tried you solution... suppose Post #1 is not published
Post::find(1) returns the post #1 discarding defaultWhere condition because find($q) method overwrites the where condition

@cebe cebe reopened this Dec 12, 2013

@cebe cebe closed this in 43f19e8 Dec 12, 2013

@cebe

This comment has been minimized.

Show comment
Hide comment
@cebe

cebe Dec 12, 2013

Member

Just checked in a fix: 43f19e8 please try again.

Member

cebe commented Dec 12, 2013

Just checked in a fix: 43f19e8 please try again.

@makroxyz

This comment has been minimized.

Show comment
Hide comment
@makroxyz

makroxyz Dec 12, 2013

Contributor

It works now! Thank you

Contributor

makroxyz commented Dec 12, 2013

It works now! Thank you

@xerxes87

This comment has been minimized.

Show comment
Hide comment
@xerxes87

xerxes87 Jan 19, 2014

I wanted to have a default sort on my index view.
I used the methods of @twinni and the sort was applied correctly.

But sorting the table on an other column isn't possible.
Is there no other way to apply default sort?

xerxes87 commented Jan 19, 2014

I wanted to have a default sort on my index view.
I used the methods of @twinni and the sort was applied correctly.

But sorting the table on an other column isn't possible.
Is there no other way to apply default sort?

@cebe

This comment has been minimized.

Show comment
Hide comment
@cebe

cebe Jan 19, 2014

Member

when using gridview or listview you can configure Sort components defaultOrder property

Member

cebe commented Jan 19, 2014

when using gridview or listview you can configure Sort components defaultOrder property

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Jan 29, 2014

After updating via composer stopped working.

defaultwhere

ghost commented Jan 29, 2014

After updating via composer stopped working.

defaultwhere

@cebe

This comment has been minimized.

Show comment
Hide comment
@cebe

cebe Jan 29, 2014

Member

we have dropped support for scopes in AR class. see #2130

Member

cebe commented Jan 29, 2014

we have dropped support for scopes in AR class. see #2130

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Jan 29, 2014

On my system has a "grouping" of data by the contractor system, ie, the authenticated user has a group id and fetch any data should be filtered by group id. I believe in doing all controllers that filter would not be legal.

ghost commented Jan 29, 2014

On my system has a "grouping" of data by the contractor system, ie, the authenticated user has a group id and fetch any data should be filtered by group id. I believe in doing all controllers that filter would not be legal.

@cebe

This comment has been minimized.

Show comment
Hide comment
@cebe

cebe Jan 30, 2014

Member

You just have to move the scope declaration to the query class or apply it in createQuery() like this:

class Post extends ActiveRecord
{
    public static function createQuery()
    {
        return parent::createQuery()->andWhere(['published' => 1]);
    }
}
Member

cebe commented Jan 30, 2014

You just have to move the scope declaration to the query class or apply it in createQuery() like this:

class Post extends ActiveRecord
{
    public static function createQuery()
    {
        return parent::createQuery()->andWhere(['published' => 1]);
    }
}
@paulhudson

This comment has been minimized.

Show comment
Hide comment
@paulhudson

paulhudson Aug 25, 2015

Seems createQuery() has now been removed.

You can now use:

 class Post extends ActiveRecord
{
    public static function find()
    {
        return parent::find()->andWhere(['published' => 1]);
    }
}

paulhudson commented Aug 25, 2015

Seems createQuery() has now been removed.

You can now use:

 class Post extends ActiveRecord
{
    public static function find()
    {
        return parent::find()->andWhere(['published' => 1]);
    }
}
@karmraj

This comment has been minimized.

Show comment
Hide comment
@karmraj

karmraj Jan 12, 2016

@cebe : I have use following code to set or apply default condition to all find() function. But in some cases i do not want to apply this condition then how can i unset this onCondtion condition in find() function.

public static function find()
{ 
    //return new PaymentQuery(get_called_class());
    return parent::find()->onCondition('fp_status = 0');
}

Thanks in Advance.

karmraj commented Jan 12, 2016

@cebe : I have use following code to set or apply default condition to all find() function. But in some cases i do not want to apply this condition then how can i unset this onCondtion condition in find() function.

public static function find()
{ 
    //return new PaymentQuery(get_called_class());
    return parent::find()->onCondition('fp_status = 0');
}

Thanks in Advance.

@samdark

This comment has been minimized.

Show comment
Hide comment
@samdark

samdark Jan 12, 2016

Member

You can't. It's a bad practice to make exceptions in this case. Either use it all the time or apply it explicitly. Else it would be hell to work with.

Member

samdark commented Jan 12, 2016

You can't. It's a bad practice to make exceptions in this case. Either use it all the time or apply it explicitly. Else it would be hell to work with.

@samdark samdark reopened this Jan 12, 2016

@samdark samdark closed this Jan 12, 2016

@faravaghi

This comment has been minimized.

Show comment
Hide comment
@faravaghi

faravaghi Apr 13, 2016

hi @samdark: How to use the 2 methods following a model: default condition for all queries and use CustomerQuery instead of the default ActiveQuery.
for example:

public static function find()
{
    return parent::find()->where(['deleted' => false]);
    // return new CustomerQuery(get_called_class());
}

faravaghi commented Apr 13, 2016

hi @samdark: How to use the 2 methods following a model: default condition for all queries and use CustomerQuery instead of the default ActiveQuery.
for example:

public static function find()
{
    return parent::find()->where(['deleted' => false]);
    // return new CustomerQuery(get_called_class());
}

@SilverFire SilverFire added question and removed question labels Apr 13, 2016

@yii-bot

This comment has been minimized.

Show comment
Hide comment
@yii-bot

yii-bot Apr 13, 2016

This is an automated comment, triggered by adding the label question.

Please note, that the GitHub Issue Tracker is for bug reports and feature requests only.

We are happy to help you on the support forum, on IRC (#yii on freenode), or Gitter.

Please use one of the above mentioned resources to discuss the problem.
If the result of the discussion turns out that there really is a bug in the framework, feel free to
come back and provide information on how to reproduce the issue. This issue will be closed for now.

yii-bot commented Apr 13, 2016

This is an automated comment, triggered by adding the label question.

Please note, that the GitHub Issue Tracker is for bug reports and feature requests only.

We are happy to help you on the support forum, on IRC (#yii on freenode), or Gitter.

Please use one of the above mentioned resources to discuss the problem.
If the result of the discussion turns out that there really is a bug in the framework, feel free to
come back and provide information on how to reproduce the issue. This issue will be closed for now.

@yiisoft yiisoft locked and limited conversation to collaborators Apr 20, 2016

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.