• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem loading up model

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:
<?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:

<?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...
               // Perform validation check
        if ($this->form_validation->run() == false)
        {    $this->load->view('failure');
                } else {
            $this->load->model('Register', '', TRUE);
} ?>

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.

[eluser]Amitabh Roy[/eluser]

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

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.

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.

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?

I removed the following 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?

[eluser]Amitabh Roy[/eluser]
The minimal correct constructor code is
function __construct(){

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.

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!

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.