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

AR, named scopes, defaultScope, relations: best practices, common pitfalls and mistakes #1642

Closed
resurtm opened this issue Nov 1, 2012 · 6 comments

Comments

@resurtm
Copy link
Contributor

resurtm commented Nov 1, 2012

Related: #1197. This is a documentation related issue.

Current guide leads some people to misunderstand concepts of the ActiveRecord, defaultScope, named scopes, relations and using them together in different combinations. Documentation definitely lacks in best practices, pitfalls and mistakes of using them.

Examples to be explained:

  1. Default scope is not a real scope as one may think. It can't be disabled at all in any circumstances.
  2. Using with inside default scope or named scope is a bad practice.
  3. Using CHttpRequest component in models is also not the best idea and reduces code testing aptitude.
  4. ...
@ErikMinekus
Copy link

I'm fairly new to Yii, and I was also doing as described in #1197, being under the impression using with inside scopes is a good practice. Is there any information (forum posts or wiki articles) we can reference right now as to why this is bad practice, or will we have to wait until the documentation is updated?

@resurtm
Copy link
Contributor Author

resurtm commented Nov 6, 2012

@ErikMinekus, i dare to offer you a describing note. Perhaps it contains mistakes, but in general conveys the idea. (Comments and refinements are appreciated.)

@rawtaz
Copy link
Contributor

rawtaz commented Apr 1, 2013

@resurtm Someone on the IRC linked to the Google Docs doc you referenced above, but it doesn't show any contents unless one is logged in. Can you please publish it in a more neutral way, e.g. a gist or something?

@resurtm
Copy link
Contributor Author

resurtm commented Apr 2, 2013

@creocoder
Copy link
Contributor

@resurtm

Using CWebUser component in models is also not the best idea and reduces code testing aptitude.

What if model contains code like:

    protected function beforeSave()
    {
        if(!parent::beforeSave())
            return false;

        if($this->isNewRecord)
            $this->created_by_id=Yii::app()->user->id;
        else
            $this->updated_by_id=Yii::app()->user->id;

        return true;
    }

What wrong here?

@resurtm
Copy link
Contributor Author

resurtm commented Apr 29, 2013

@creocoder, nothing wrong. It was my mistake, sorry. I actually meant CHttpRequest application component. One day i came across the following code:

protected function beforeSave()
{
    if(!parent::beforeSave())
    {
        return false;
    }

    $this->user_agent=Yii::app()->request->userAgent; // 0_o?
    $this->remote_addr=Yii::app()->request->userHostAddress; // 0_o?

    return true;
}

It is definitely a bad practice. I've changed the issue description.

@samdark samdark closed this as completed Mar 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants