I’m writing an mvc web app with the yii framework. I have a piece of business logic and I’m unsure where to place it. $usernameid=$model->random_id_gen('5'); is the function I’m talking about.
SiteController:
<!-- snip -->
public function actionIndex()
{
$model = new Users();
if (isset($_POST['Users'])) {
//call the active record table model
$model = new Users();
//massively assign attributes
$model->attributes=$_POST['Users'];
//generate random userid
$usernameid=$model->random_id_gen('5');
<!-- snip -->
Users Active Record Class:
<!-- snip -->
public function random_id_gen($length)
{
$characters = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
$max = strlen($characters) - 1;
$string = '';
for ($i = 0; $i < $length; $i++) {
$string .= $characters[mt_rand(0, $max)];
}
return $string;
}
My question: Does this id generator function belong in the Active record model? Should it be in the controller? Should it be in a separate model since it is “business logic”, but has little to do with databases?
I’m trying to get better at not “bloating” my MVC classes. Thanks ahead of time guys.
UPDATE
I’m looking for a yii specific solution. It seems the question has developed into “where should one put libraries in yii” if there is such a convention.
MVC should be renamed to MVCL .. that is Model-View-Controller-Library. These kind of functions should go into a separate system of libraries.
Controller should be on the TOP. It has two major functions : accepting user input and coordinating other elemnts (controller / library/view) for output. traditionally, Controller asks data from the model. But data manipulation (most common) should be done by functions in the library.
There could be two levels of libraries, one could be complex (object based) and other could be simple (global functions based) for the most common operations. Something like stripping out tags, OR making the data XML ready, cleaning an URL etc.. simple functions libraries can be accessed by the whole system without any instantiation of the a particular library object.
Makes sense ?