Welcome Guest, Not a member yet? Register   Sign In
Examples of Jamie Rumbelow's My_Model in Action?
#11

[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 Smile

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>&lt;?= $book->title ?&gt; - &lt;?= $book->author ?&gt;</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:
&lt;?= form_open('books/create') ?&gt;
    <p>Title: &lt;?= form_input('book[title]') ?&gt;</p>
    <p>Author: &lt;?= form_input('book[author]') ?&gt;</p>
&lt;?= form_close() ?&gt;

...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!


Messages In This Thread
Examples of Jamie Rumbelow's My_Model in Action? - by El Forum - 08-27-2012, 01:17 PM
Examples of Jamie Rumbelow's My_Model in Action? - by El Forum - 08-27-2012, 05:26 PM
Examples of Jamie Rumbelow's My_Model in Action? - by El Forum - 08-28-2012, 07:45 AM
Examples of Jamie Rumbelow's My_Model in Action? - by El Forum - 08-28-2012, 08:47 AM
Examples of Jamie Rumbelow's My_Model in Action? - by El Forum - 08-28-2012, 09:09 AM
Examples of Jamie Rumbelow's My_Model in Action? - by El Forum - 08-28-2012, 10:05 AM
Examples of Jamie Rumbelow's My_Model in Action? - by El Forum - 08-28-2012, 01:04 PM
Examples of Jamie Rumbelow's My_Model in Action? - by El Forum - 08-28-2012, 01:06 PM
Examples of Jamie Rumbelow's My_Model in Action? - by El Forum - 08-28-2012, 02:40 PM
Examples of Jamie Rumbelow's My_Model in Action? - by El Forum - 08-28-2012, 06:33 PM
Examples of Jamie Rumbelow's My_Model in Action? - by El Forum - 08-29-2012, 08:32 AM
Examples of Jamie Rumbelow's My_Model in Action? - by El Forum - 08-29-2012, 08:56 AM
Examples of Jamie Rumbelow's My_Model in Action? - by El Forum - 08-29-2012, 09:00 AM
Examples of Jamie Rumbelow's My_Model in Action? - by El Forum - 08-29-2012, 09:13 AM
Examples of Jamie Rumbelow's My_Model in Action? - by El Forum - 03-14-2013, 02:47 AM
Examples of Jamie Rumbelow's My_Model in Action? - by El Forum - 03-14-2013, 07:25 AM
Examples of Jamie Rumbelow's My_Model in Action? - by El Forum - 09-18-2014, 07:53 PM



Theme © iAndrew 2016 - Forum software by © MyBB