creocoder / yii2-nested-sets

The nested sets behavior for the Yii framework.
Other
446 stars 129 forks source link

Is Separate Query Behavior Really Necessary? #61

Closed angelcoding closed 9 years ago

angelcoding commented 9 years ago

I'm just wondering why a separate Query behavior is needed when you could simply add those functions to the main AR behavior and just use that without having to extend ActiveQuery and add a second behavior? e.g move roots function to NestedSetsBehavior (would be similar for leaves() function too) ...

    /**
     * Gets root node(s).
     * @return ActiveQuery.
     */
    public function roots()
    {
        $query = $this->owner->find();

        $query->andWhere([$this->leftAttribute => 1])
              ->addOrderBy([$this->owner->primaryKey()[0] => SORT_ASC]);

        return $query;
    }

Am I wrong in thinking this?

creocoder commented 9 years ago

Its absoluttely neccesary. Moreover its normal to have Query model if you want scopes feature for example. Also look query leaves() and roots() usage documentation. Its work like Category::find()->roots() or Category::find()->leaves() while ActiveRecord behavior need instance. We do not have instance in such cases.

angelcoding commented 9 years ago

Aaah OK, thanks for clearing that up. I can see the advantage now when using static class methods.