Mysql View In Yii

Hi all! I created 2 mysql views, and has generated from them 2 model.

MostPopularCoupon


class MostPopularCoupon extends CActiveRecord

{

    public function tableName()

    {

        return 'most_popular_coupon';

    }


    public function rules()

    {

        return array(

            array('coupon_id', 'required'),

            array('coupon_id', 'numerical', 'integerOnly'=>true),

            array('left_coupons', 'length', 'max'=>22),

            array('stopped_at', 'safe'),

            array('left_coupons, coupon_id, stopped_at', 'safe', 'on'=>'search'),

        );

    }


    public function relations()

    {

        return array(

        );

    }


    public function attributeLabels()

    {

        return array(

            'left_coupons' => 'Left Coupons',

            'coupon_id' => 'Coupon',

            'stopped_at' => 'Stopped At',

        );

    }


    public function search()

    {

        $criteria=new CDbCriteria;

        $criteria->compare('left_coupons',$this->left_coupons,true);

        $criteria->compare('coupon_id',$this->coupon_id);

        $criteria->compare('stopped_at',$this->stopped_at,true);


        return new CActiveDataProvider($this, array(

            'criteria'=>$criteria,

        ));

    }


    public static function model($className=__CLASS__)

    {

        return parent::model($className);

    }

}

MostActiveCity


class MostActiveCity extends CActiveRecord

{

    public function tableName()

    {

        return 'most_active_city';

    }

    public function rules()

    {

        return array(

            array('mines', 'length', 'max'=>21),

            array('city', 'length', 'max'=>255),

            array('mines, city', 'safe', 'on'=>'search'),

        );

    }


    public function relations()

    {

        return array();

    }


    public function attributeLabels()

    {

        return array(

            'mines' => 'Mines',

            'city' => 'City',

        );

    }


    public function search()

    {

        $criteria=new CDbCriteria;


        $criteria->compare('mines',$this->mines,true);

        $criteria->compare('city',$this->city,true);


        return new CActiveDataProvider($this, array(

            'criteria'=>$criteria,

        ));

    }

    public static function model($className=__CLASS__)

    {

        return parent::model($className);

    }

}

But due to no experience, don’t know how to use them correctly. I need to substitute company_id in the query to get the data of the company belongs to the current user…

Sorry, not really sure what you are asking. Which ‘view’ needs company_id? substitute how? which table is company_id in? If you need different data in the MySQL view, you need to change the view and then regenerate the model.

I need different data for company_id where company_id = Yii::app()->user->company_id

i no uderstend how i can use this in conjunction with MostActiveCity and MostPopularCoupon since fields company_id not in these models. I can make use of without re-generating models?

I think I’m getting an idea of what you are trying to.

You have a user that wants to check information on their coupon campaign. I assume that they don’t need to edit any of this information, just view it?

If they have to edit the information, then the MySQL views need to be modified to include company_id, and then company_id added to the model.

Views are pre-compiled in MySQL and if you have a LOT of records involved in the base tables for the above 2, it is an optimization for the DB. If the user just needs to view the data, a stored-procedure with parameters would also work.

You could also look into named scopes in the base-table models.

then just send in the company_id as the parameter.

Hope this helps.