在将对象传递给模型之前,是否值得在控制器中创建对象


Is it worth creating an object in the controller before passing it off to the model?

我正在Codeigniter中创建一个公文包页面,我还为该网站配备了一个简单的CMS。

我有一个控制器,它创建投资组合项目如下:

public function create_portfolio()
{
    $this->load->model('portfolio_model');
    $this->portfolio_model->insert(
        $this->input->post('title'),
        $this->input->post('short_description'),
        $this->input->post('complete_description'),
        $this->input->post('github'),
        $this->input->post('url')
    );
}

一种面向对象的方法是这样的:

public function create_portfolio()
{
    $this->confirm_login();
    $this->load->model('portfolio_model');
    $portfolio = new Portfolio(
        $this->input->post('title'),
        $this->input->post('short_description'),
        $this->input->post('complete_description'),
        $this->input->post('github'),
        $this->input->post('url')
    );
    $this->portfolio_model->insert($portfolio);
}

随着雇主越来越多地寻找那些具有OOP技能的人,我试图评估后一种方法是否有效。最后,我拥有的模型最终会访问Portfolio对象中的所有实例变量,以便插入数据库(我没有使用ORM)。

在将所有输入字段传递到模型之前,是否有必要将其实际分组到一个对象中?我可能把这个问题说错了,但我希望你能提供意见。

由于您将值传递给构造函数,构造函数可能会对输入进行验证以确保其有效,或者在

$portfolio->title可能不等于$this->input->post('title');

因此,选择2会更好。