Welcome Guest, Not a member yet? Register   Sign In
CI4 MVC DB accesses are often made in the controller in the examples
#4

(01-04-2021, 05:15 AM)includebeer Wrote: This is explained in the tutorial in the user guide:
http://codeigniter.com/user_guide/tutori...ction.html
http://codeigniter.com/user_guide/tutori...items.html

What do you find confusing?
In the tutorial, the "save" method is called in the "news" controller.
This is a database access and according to my understanding it should only take place in the "model".
Am I correct?

public function create()
{
$model = new NewsModel();

if ($this->request->getMethod() === 'post' && $this->validate([
            'title' => 'required|min_length[3]|max_length[255]',
            'body'  => 'required'
        ]))
{
$model->save([                   <------------------
'title' => $this->request->getPost('title'),
'slug' => url_title($this->request->getPost('title'), '-', TRUE),
'body' => $this->request->getPost('body'),
]);

echo view('news/success');
}
else
{
echo view('templates/header', ['title' => 'Create a news item']);
echo view('news/create');
echo view('templates/footer');
}
}
Reply


Messages In This Thread
RE: CI4 MVC DB accesses are often made in the controller in the examples - by divebase - 01-06-2021, 04:11 AM



Theme © iAndrew 2016 - Forum software by © MyBB