Welcome Guest, Not a member yet? Register   Sign In
from Controller to Model
#1

[eluser]rvent[/eluser]
The way i understand it is that a Controller has all the processing and validation etc, and the model should contain all the insert, updates, delete, etc that affect the DB. (technically according to the guide)

i have this controller
Code:
<?php

class Authorsc extends Controller {
    
    function Authorsc
    {
        parent::Controller();
        
        $this->load->database('default');
        $this->load->model('Authorsm');
    }
    
    function createAuthor()
    {
        
    }
}
?>

And i have this model:
Code:
<?php

class Authorsm extends Model {
    
    var $Author = '';
    
    function Authorsm()
    {
        parent::Model();
    }
    
    function insertAuthor()
    {
        $this->Author = $_POST['Author'];
        $this->db->insert('MessageAuthor', $this);
    }
    
    function showAuthors()
    {
        $authors = $this->db->get('MessageAuthor');
        return $authors->result();
    }
}
?>

So in order to get a form and enter the data i need i would have to have a view loaded by the controller and that contains something like this:
example view:
Code:
<?=form_open('MODEL/FUNCTION');?>
<?=form_hidden('author_id', $this->uri->segment(3));?>

????

The video tutorial says to use <?=form_open('CONTROLLER/FUNCTION');?>, but i want to be able to get the data from the form into the controller, do any validation or anything else i want and then give it to the model so that it can be inserted into the database...

Can someone give me insites or any ideas...?

Thanks
#2

[eluser]Michael Wales[/eluser]
The controller handles all user input (whether it be a form, a URL to your site, etc). Your controller would look something like this:

Code:
function createAuthor() {
  $this->load->library('validation');
  $rule['name'] = 'trim|required|max_length[40]'; // The authors name is required, max length of 40, kill whitespace
  $this->validation->set_rules($rules);
  $fields['name'] = 'Author Name';
  $this->validation->set_fields($fields);

  if ($this->validation->run()) {
    // Our validation run - send the data to the model for DB input
    $this->load->model('authorsm');
    $this->authorsm->createAuthor($_POST);
  } else {
    // Either our validation hasn't run - or it failed when they submitted the form
    $this->load->view('ourform');
  }
}

Model:
Code:
function createAuthor($input = array()) {
  $name = $_POST['name'];
  $this->db->insert('authors', array('name'=>$name));
}

Of course, this can be cleaned up a bit and you would want to add some more error checkign and degredation upon unexpected errors, but you get the point.
#3

[eluser]rvent[/eluser]
No worky for me... Everything looks like it is working, but the value is not being assigned...

here is what i have..
controller:
Code:
<?php

class Authorsc extends Controller {
    
    function Authorsc()
    {
        parent::Controller();
        
        $this->load->database('default');
        $this->load->model('Authorsm');
        
        $this->load->helper(array('form', 'url'));
    }
    
    function index()
    {
        $this->load->view('authorsv');
    }
    function createAuthor()
    {
        $this->load->library('validation');
        
        $rules['author'] = 'trim|required|max_length[40]';
         $this->validation->set_rules($rules);
    
          $fields['author'] = 'author';
          $this->validation->set_fields($fields);

          if ($this->validation->run())
        {
            $this->load->model('authorsm');
            $this->authorsm->insertAuthor($_POST);
          }
        else
        {
            $this->load->view('success');
         }    
    }
}
?>

model:
Code:
<?php

class Authorsm extends Model {
    
    var $Author = '';
    
    function Authorsm()
    {
        parent::Model();
    }
    
    function insertAuthor($input = array())
    {
        $this->Author = $_POST['Author'];
        $this->db->insert('MessageAuthor', array('Author' => $this->Author));
    }
    
    function showAuthors()
    {
        $authors = $this->db->get('MessageAuthor');
        return $authors->result();
    }
}
?>

view
Code:
<html>
    <head>
        <title>Test</title>
    </head>
    
    <body>
    
        <?=form_open('authorsc/createAuthor');?>
        <?=form_hidden('AuthorID');?>
        <p>&lt;input type="text" name="author" /&gt;</p>
        <p>&lt;input type="submit" name="submit" /&gt;</p>
        &lt;/form&gt;
        
    &lt;/body&gt;
&lt;/html&gt;

view on success:
Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;My Form&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

<h3>Your form was successfully submitted!</h3>

<p>&lt;?=anchor('authorsc', 'Try it again!'); ?&gt;</p>

&lt;/body&gt;
&lt;/html&gt;

I get the form, i enter a name click on submit and i get the following:
Code:
An Error Was Encountered
Error Number: 1048

Column 'Author' cannot be null

INSERT INTO MessageAuthor (Author) VALUES (NULL)

Any thoughts..?

Thanks
#4

[eluser]rvent[/eluser]
never mind it was because of Author instead of author on the _POST

thanks




Theme © iAndrew 2016 - Forum software by © MyBB