Welcome Guest, Not a member yet? Register   Sign In
Splitting responsibilities between controller and model
#1

(This post was last modified: 03-04-2024, 03:58 AM by bobw.)

I'm trying to work out the best way to handle the balance between controllers and models when migrating from CI3 to CI4.

It seems like the old system used the DataMapperExt class as the access to the underlying database. So previously I'd have:

PHP Code:
class Member extends DataMapperExt {
    ...



Then that would be instantiated by a Member_model class like:

PHP Code:
class Member_model extends CI_Model {
    ...
    protected function add($data) {
        $m = new Member();
        // validate and/or clean data
        // i.e. user might enter their name as "smith", "SMITH", etc whereas the database holds it as "Smith"
        return $m->save();
    }
    ...



Then the controller would really just forward the data on from the form:

PHP Code:
class Member extends CI_Controller {
    $mem = new Member_model();

    public function add() {
        $data $this->input->post(NULLTRUE);

        $this->mem->update($data);
    }



In CI4 it appears that the validation and tidying up is done in the controller, or at least that's how the example at https://www.codeigniter.com/user_guide/t...items.html does it but the notes on models at https://www.codeigniter.com/user_guide/m...validation suggests that you can do it within the model itself. The latter seems a bit cleaner to me.

The user model in CI4 now simply extends the core model detailing which database columns may be modified and whether there should be any validation on specific columns to make sure that someone doesn't try to enter too long a name or a negative age for example. That would mean my Member model now looks like:

PHP Code:
class Member extends Model {
    protected $table 'members';

    protected $primaryKey 'id';

    protected $useAutoIncrement true;

    protected $allowedFields = ['given_name''family_name''gender''age'];

    protected $validationRules = [
        'given_name'      => 'required|max_length[30]|alpha_numeric_space|min_length[2]',
        'family_name'    => 'required|max_length[30]|alpha_numeric_space|min_length[3]',
        'age'          => 'required|is_natural',
        ...
    ];


The controller would look similar to this...

PHP Code:
class Member extends BaseController {
    helper('form');

    public function add_member() {
        $data $this->request->getPost(['given_name''family_name''gender''age']);

        // assume that this does the titlecase manipulation amongst other things.
        $cleanData $this->tidyData($data);

        $model model('members');

        if (!$model->save($cleanData)) {
            // handle error
        }
        // display appropriate view
    }



Have I got the above right? My thoughts are to have the validation done within my models as it's then available to whichever controller ends up using it.
Reply


Messages In This Thread
Splitting responsibilities between controller and model - by bobw - 03-04-2024, 03:47 AM



Theme © iAndrew 2016 - Forum software by © MyBB