CFilter or Another Solution

I have reading the API Docs. There are answers that seems to be clear to my situation, but i am not sure yet.

When the actionCreate() of Class X runs, I need to execute an extra algorithm to find similars ‘descriptions’ in the dataset.

The same situation happens when someone edit one data row. I need pass the algorithm again, searching for duplicate or similars descriptions.

My question is: Where would be suitable to implement the algorithm?

  • In the actionCreate() and actionUpdate().

  • Implement a Meta Controller with the code.

  • Implement a CFilter to be execute before and/or after a controller action executes.

The last one seems the better choice, but there is not much information on the API Docs about.

What is your opinion…?

Similarly appreciate a example of using CFilter Class.


Do you need to display results to a user or just find similar data? For displaying you can use a widget + an additional controller method if you would like to do it using ajax. The algorithm logic should go to a model, so that you can execute it from anywhere and not only from a one controller. Also a model’s method (e.g. findSimilarDescriptions) looks more logical than the same method of a controller.

If the description have not similars, the system handlers at will. But if the algorithm find any similar data, the user get a notification of PENDING description. Then the administrator will decide what he going to do with that entry (accept or deny).

Thanks Andy anyway, this morning i was thinking to put the algorithm as a static function on model, because i will need it in some other class model. Maybe to execute it on afterSave() method.

Appreciate some comments about this last idea.

If this algorithm can be applied to several models, then you should consider making a separate component or put it into a base ActiveRecord class or even create a CActiveRecordBehavior so that you can attach it to any AR object at any time.