[eluser]Jamie Rumbelow[/eluser]
Matthew, I'm posting this here rather than replying to your email so it can help others if it needs to
MY_Model really doesn't require anything out of the ordinary for creating apps in CodeIgniter. You use its methods in your controller to access data from your database easily and then render that data out in your view.
For instance, let's implement the show method in a regular RESTful controller:
Code:
public function show($id)
{
$this->data['book'] = $this->book_model->get($id);
$this->load->view('books/show', $this->data);
}
Inside
books/show we now have access to a
$book object which represents the current book:
Code:
<h1><?= $book->title ?> - <?= $book->author ?></h1>
Creating a new book is easy too. We can pass through the data directly from the form input if we want to. Our form can look like this:
Code:
<?= form_open('books/create') ?>
<p>Title: <?= form_input('book[title]') ?></p>
<p>Author: <?= form_input('book[author]') ?></p>
<?= form_close() ?>
...and then in our controller:
Code:
public function create()
{
$book = $this->input->post('book');
if ($id = $this->book_model->insert($book))
{
redirect('books/' . $id);
}
}
This is the easiest way, although it does open up the model for mass-assignment, so it's not particularly safe. To prevent mass-assignment you could specify the values you pass into the model specifically:
Code:
public function create()
{
$data = $this->input->post('book');
$book = array(
'title' => $data['title'],
'author' => $data['author']
);
if ($id = $this->book_model->insert($book))
{
redirect('books/' . $id);
}
}
I'm working on a way to add 'protected attributes' as a concept and thus prevent mass assignment. That'll be in
MY_Model 2.0.
Hope this helps!