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 vs query builder #134

Open
samdark opened this issue Jul 21, 2016 · 5 comments
Open

AR vs query builder #134

samdark opened this issue Jul 21, 2016 · 5 comments

Comments

@samdark
Copy link
Owner

samdark commented Jul 21, 2016

AR is perfect when you need to delete, update or create one or more records sequentially. Its support of dirty attributes (saving only what was really changed) results in optimized UPDATE statements which lifts the load from database significantly and reduces chances for various conflicts connected with editing same record by multiple persons at the same time.

If you don't have really complex logic in your application and therefore it doesn't require abstracting entities, AR is the best fit for deletes, updates and creates.

AR is also OK for simple queries resulting in under 100 records per page. It's not as performant as working with arrays produced by query builder or asArray() but is more pleasure to work with.

AR is not recommended for complex queries. These are usually about aggregating or transforming data so what's returned doesn't fit AR model anyway. It is preferable to use query builder in this case.

Same goes for import and export. Better to use query builder because of high amounts of data and possibly complex queries.

@cherifGsoul
Copy link

@samdark what you think about using Query Builder that uses Active Query which returns Arrays instead of AR object?

For now I use AR for writing (Update, Delete), and query builder for fetching data.

@samdark
Copy link
Owner Author

samdark commented Nov 2, 2016

I think it is good.

@cebe
Copy link
Contributor

cebe commented Nov 8, 2016

for import it may still be useful to use AR for validating imported data.

@ivansal
Copy link

ivansal commented Nov 8, 2016

Also if need to trigger AR events

@lav45
Copy link

lav45 commented Nov 8, 2016

Retrieving related data lists. As an example getCategoryList()

/**
 * @property integer $category_id
 * @property Category $category
 */
class Post  extends ActiveRecord
{
    public function rules()
    {
        return [
            [['category_id'], 'in', 'range' => array_keys($this->getCategoryList())],
        ];
    }

    public function getCategoryList()
    {
        return Category::find()
            ->select(['name'])
            ->indexBy('id')
            ->active()
            ->column();
    }

    public function getManager()
    {
        return $this->hasOne(Category::class, ['id' => 'category_id']);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants