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!
#12

[eluser]MatthewSchenker[/eluser]
Hi Jamie,
Thank you very much for jumping in to help! I really appreciate it.

The one query I seem to be stuck on is updating using My_Model -- obtaining the current value from the database and passing that value back and forth into form fields in the view for editing.

Thanks again for your help.

Matthew
#13

[eluser]Jamie Rumbelow[/eluser]
Updating is simple! Get the object in our edit action:

Code:
public function edit($id)
{
    $this->data['book'] = $this->book_model->get($id);

    // render view blah
}

Have our form in the view output the values:

Code:
&lt;?= form_open('books/update/' . $book->id) ?&gt;
    <p>Title: &lt;?= form_input('book[title]', $book->title) ?&gt;</p>
    <p>Author: &lt;?= form_input('book[author]', $book->author) ?&gt;</p>
    <p>&lt;?= form_submit('', 'Update') ?&gt;</p>
&lt;?= form_close() ?&gt;

When the user submits the form:

Code:
public function update($id)
{
    $data = $this->input->post('book');

    $book = array(
         'title' => $data['title'],
         'author' => $data['author']
    );
    
    $this->book_model->update($id, $book);
    redirect('books/' . $id);
}

Easy!
#14

[eluser]MatthewSchenker[/eluser]
Greetings Jamie,
AHA!

Seriously...

Thanks for providing one of those moments...

Matthew
#15

[eluser]daBayrus[/eluser]
Hi Jamie,

How do you use the function with() or relate()?

Thanks,
daBayrus
#16

[eluser]Renato Araujo[/eluser]
But the coolest use of My_model jamierumbelow and use with My_Controller himself.

https://github.com/jamierumbelow/codeign...controller

I'm using in a project and helped me too.
#17

[eluser]Unknown[/eluser]
[quote author="Jamie Rumbelow" date="1346254357"]...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...[/quote]

I've been doing some reading about CodeIgniter (ActiveRecord, really) and mass-assignment vulnerabilities. I'm curious...are the keys of the assoc array $book how you're suggesting to specify the table fields to prevent mass-assignment? Is specifying the field the simplest way to avoid it or would not passing arrays via the HTTP POST do the same thing?

Code:
public function create()
{
    $title = $this->input->post('title');
    $author = $this->input->post('author');
    
    if ($id = $this->book_model->insert(array($title, $author)))
    {
        redirect('books/' . $id);
    }
}

or would this still have to change to

Code:
// ...
if($id = $this->book_model->insert(array('title'=>$title, 'author'=>$author)))
// ...




Theme © iAndrew 2016 - Forum software by © MyBB