Welcome Guest, Not a member yet? Register   Sign In
Problem loading up model
#1

[eluser]sakamofo[/eluser]
I'm having so much trouble loading up a model. I'm really new at CodeIgniter, this is my first time working with a PHP framework.
Currently, my main model has this inside it:
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Register extends CI_Model {

    public function registerUser() {    
        $insert = array(
           // Database key => value pairs
        );
        $this->db->insert('user', $insert);
    }
}

?>

And inside my controller, I have this:

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Registration extends CI_Controller {
    
    public function index() {    
        $this->load->helper(array('form', 'url'));
        $this->load->library(array('form_validation', 'encrypt', 'session'));
                    
        $config = // ...array validation rules...
        $this->form_validation->set_rules($config);
            
               // Perform validation check
        if ($this->form_validation->run() == false)
        {    $this->load->view('failure');
                } else {
            $this->load->model('Register', '', TRUE);
            $this->Register->registerUser();
            $this->load->view('success');
        }
        
    }
} ?>


But for some reason it's not loading anything up even when no validation errors occur and if there's a view file being loaded; just a blank page. I've loaded the model the same as the documentation/user-guide but it's not working for me. When I take out the model load in the controller, it works just perfectly (without validation errors of course).

I've used $this->load->database(), loaded in both the model and controller, and my configuration in the database config file is correct. I never usually ask for help but this problem has definitely stumped me.

Can someone please assist me? I would be absolutely appreciative.
Thank you, Sakamoto.
#2

[eluser]Amitabh Roy[/eluser]
Try,

Code:
$this->load->model('register', '', TRUE);
$this->register->registerUser();

make sure you model file name is in lowercase, which should be in this case register.php
located at application/models/register.php



Also noted you are missing the constructor in your controller.

Code:
function __construct()
{  }
Check the "Anatomy of a Model" section for naming convention in the Codeigniter user guide page for models for the uppercase/lowercase convention.
http://ellislab.com/codeigniter/user-gui...odels.html
#3

[eluser]sakamofo[/eluser]
I just tried your solution, but still no luck. I've read the User Guide prior to posting this as well and unfortunately both uppercase/lowercase conventions don't make a difference, at least in my case.

I placed a __construct() call into the model page (register.php) and I'm still getting a blank page when the form is submitted and nothing is inserted into the database.

Any suggestions?
#4

[eluser]sakamofo[/eluser]
I removed the following code:
Code:
if (!defined('BASEPATH')) exit('No direct script access allowed');

And it is now working perfectly! However, is there any other way of disallowing direct script access?
Is that code even necessary in Model scripts?
#5

[eluser]Amitabh Roy[/eluser]
The minimal correct constructor code is
Code:
function __construct(){
                parent::__construct();
            }


Also
Code:
if (!defined('BASEPATH')) exit('No direct script access allowed');

Am not sure why including the above code doesn't work for you, need to look into your structure in more details. It has been working fine for me.
#6

[eluser]sakamofo[/eluser]
I'm not exactly sure why that particular code doesn't work either. Time to continue working with CI, thank you very much for your help!




Theme © iAndrew 2016 - Forum software by © MyBB