How to restrict certain row in the database to appear anywhere in the portal?

I am need of a method that will prevent a row to be displayed anywhere in the portal when for a certain user I match a column in the database.

For example in pseudo code it would be:

  • when user has ‘upgraded’ never show tips for upgrading

I have ‘user’ and ‘tips’ models, and I have the mapping between them.

How can I make sure that from ‘tips’ those items are never displayed on any page in the portal. I am looking to achieve this, if possible by editing the models only, and not the controllers.

Is it possible?

The simplest idea I could think about right now is to have a filed in the user table that would hold the ID of the tip he has completed last… in your example the user that has upgraded would hold the ID of the "Please upgrade" tip…

This way you can always show only non-completed tips…

This assumes that tips are serial… ie. you need to complete them all one by one and in order…

if you would like the user to choose what tip to complete… than a 3rd table is needed where you would hold user_id and tip_id…

You got it wrong. I know how to represent this in the database.

My question was how to code this on an abstracted level (possibly in the model) that find() and search() never return anything that is not relevant. I need a way to restrict some table-records per-user to never ever show up in the system.

Is it clear now?

In this case… it would help if you had explained how you represent the data… do you use something I described… the first option?.. or the second?..

For the first option… if you have in user table a field "tip_id"…

in the model you would make a method… getNewTips()… that would do something line


Tips::model()->find('id>:id',array(':id'=>$user->tip_id)); 

I do not want to change all the pages that make use of the model, I even don’t know how many are, and there is team-work so it might break in future, if other programmers don’t follow the protocol.

So for this situation is not viable doing like you just posted, because that time I have to alter all of the existing code where these items are displayed.

I want to run something for each access, that means I want to run a filter on the model before running any operation.

Something like beforeFind, but this should be globally and abstracted on the model class.

It all depends on your current data and code… how do you currently find which tips to display?

I supose you are now using some relation like $user->tips… by my above solution you would use $user->newtips…

On the other side… you can set a defaultScope() for the tips - http://www.yiiframework.com/doc/api/1.1/CActiveRecord#defaultScope-detail, this way you don’t need to make many changes…

[color=#222222][font=Arial, sans-serif][size=2]defaultScope sounds promising, I will see if this feature resolves my issues.[/size][/font][/color]