在MVC模式中转换表单数据的最佳位置

Let's say that model insert data to database.

class Model
{
    public function insert($value) {
        // putting $value to database to integer field
    }

}

I want to convert $value to int by (int) $value to prevent the insertion of the non-numeric value.

Where should I do this? In model method or in controller?

class Controller
{
    public function indexAction() {
        $model = new Model;
        $model->insert($_POST['user_id']);
    }
}
  1. It hardly makes any difference to PHP whether a value is a numeric string or an integer, it'll just work with it the same way and convert implicitly when actually necessary.
  2. If this is actually a concern though, the controller is logically the right place to do it. Numbers being numeric strings instead of integers is a problem unique to the method of input, and a controller is an "input adapter" if you will which takes input in a specific form and passes it through to the model in a standardised format.

Controller just takes user input and provides data to a model. Controller shouldn't know nothing about databases and storing formats. Theoretically you can change one DB to another with another data types and it will be problem of your model, not controller.

you should have to do it in controller only... bcz this is best practice i can say...

so from controller you can convert $value in to (int) form and send

you can do it in controller

 $data = (int) $value;
 $model->interst($data);