Adrian Gunawan
Adrian Gunawan

Reputation: 14429

Yii - create an adhoc model

I'm asking for a best practice to create an adhoc model. I want to create a model 'menu' which does not come from a database table but hardcoded in the code instead.

The reason is that i can call the menu model from two different view/layouts.

I tried this as a starting point.

/model/Menu.php

class Menu extends CModel
{
    public getMenu() {
        return array('home'=>'home/index',
                     'product'=>'product/index',
                     'order'=>'order/index',
        );
    }
}

Do you think this is bad or is there a better way to do this?

Thanks

Upvotes: 0

Views: 184

Answers (2)

mjalajel
mjalajel

Reputation: 2201

I'd suggest creating it as a class just like you did, but I wouldn't extend CModel to avoid the unnecessary overhead. Something like:

class Menu{
    public static getMenu() {
        return array('home'=>'home/index',
                     'product'=>'product/index',
                     'order'=>'order/index',
        );
    }
}

Upvotes: 1

rinat.io
rinat.io

Reputation: 3188

I think it should be a widget, not model. Or even partial view. Depends on how you going to use it.

Upvotes: 1

Related Questions