CodeIgniter Forums
Trouble with Models - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Trouble with Models (/showthread.php?tid=6892)

Pages: 1 2


Trouble with Models - El Forum - 03-16-2008

[eluser]Dan Decker[/eluser]
I'm having a bear of a time getting a model to work. When the model is not loaded, everything works fine. If I load the model anywhere (autoload.php, in the Controller file), it seems to cause the script to stop processing.

I have loaded the model after an echo statement to verify that loading the model is waht is breaking things. I have even built a very bare bones model (testmodel.php whose only purpose was to generate an echo) and even trying to load it broke things.

Environment info: PHP 5.2.5, MySQL 5.0.41 via MAMP on a 2.33 Core2 Duo 24" iMac, 2GB RAM, Mac OS X v10.5.2

Here is my code:
action.php takes input from a form and attempts to log in the user --
Code:
<?php
/*
|The "Action" Controller will do various actions like login and logout the user.
|Currently the plan is that the Action COntroller will also handle various |Ticket entry/edit tasks. This may change based on semantics (are these things |related in some way? Should they be broken out into their own Controller?
*/
class Action extends Controller {

    function Action()
    {
        parent::Controller();
        
    }
    
    function index()
    {
        //Routes to an "Error Message", prevents direct script access.    
        $this->load->view('error_message');
    }
    
    function login()
    {
        /*
        |Process the Login values against the database (Database class is
        |auto-loaded via autoload.php)
        |
        |Set the user session (Session calss is auto-loaded via autoload.php)
        |
        |Load the main Ticket management view
        */
        $this->load->model('User_model');
        
        if ($this->User_model->user_login())
        {
        
        $this->load->view('tickets');
        
        } else {
        
        $this->load->view('error_message');
        
        }
        
    }
    
}
?>
And here is the model, user_model.php that is called by action.php --
Code:
<?php
/*
|This model will handle User based functions. Login/Logout and user account |creation/edit/deletion.
|This model is called by the Action Controller
*/
class User_model extends Model {
    
    function User_model()
    {
        parent::Model();
    }

    function user_login()
    {
        //Security helper is auto-loaded via autoload.php
        $u_name = $this->input->post('u_name');
        $p_word = dohash($this->input->post('p_word'), 'md5');
        
        $query = $this->db->get_where('ls_ticket_users', array('u_name' => $this->u_name));
        $u_check = $query->result();
        
        if ($u_check['p_word'] == $this->p_word)
        {
        return TRUE;
        } else {
        return FALSE;
        }
    }

}

?>

Is there just something simple that I'm missing? I've stared at it for so long that I just can't tell. This is my first attempt at a CI based app, I've done all this is naked php before, but I'm a noob here. BTW, I've verrified that I can get the desired info out of my database, but it dorks up when I try to apply it in the model.

My only theory at this point is that Models are broken in CI with 1.6.1, but I KNOW that's not true ;-)

Let me know if there is anything else I can provide. I'd love to get this worked out, I love CI!


Trouble with Models - El Forum - 03-16-2008

[eluser]wiredesignz[/eluser]
Do you get errors? Or just a blank page?

A blank page suggests whitespace before or after your PHP tags.


Trouble with Models - El Forum - 03-16-2008

[eluser]Dan Decker[/eluser]
No errors, just a blank page. I'll make sure there is no whitespace before or after the opening and closing
Code:
<?php -- ?>

I'll post back my results, thanks for the info!


Trouble with Models - El Forum - 03-16-2008

[eluser]Dan Decker[/eluser]
Sorry to say I've checked for any white space anywhere near the php tags, and in the view file, and I still get just a blank page.

Did the code above look like it should work? Is there anything else to consider?

Thanks in advance,


Trouble with Models - El Forum - 03-16-2008

[eluser]xwero[/eluser]
There are two common reasons for having blank pages
- white space, extra line behind the closing php tag
- compress_output config option set to true

The first problem can be prevented by not using the closing php tag in models and controllers.
The second problem is the server configuration that does react well compressing the data. It is set to false by default for this reason.

I saw something in the sample
Code:
$this->load->model('User_model');
From the userguide
Quote:When a model is loaded it does NOT connect automatically to your database
So it's best to set the third parameter of the model method to TRUE to make sure the database connection is loaded.
Code:
$this->load->model('User_model','',TRUE);



Trouble with Models - El Forum - 03-16-2008

[eluser]Dan Decker[/eluser]
Thanks for the ideas.

Should I pass the third paramater even though I have the database class autoloaded? This is likely a misunderstanding on my part, as running the query outside of the Model didn't seem to require any extra effort asside from having the DB class autoloaded. I see where there would be no harm in passing TRUE just as a guarantee.

Seriously, can the php tags be completely removed from Controllers and Models? I noticed this was the pattern in the examples used in the docs. I just assumed it was implied that we were to use them. I'll remove them and see what happens!

Thanks to everyone, I'll post back with results.


Trouble with Models - El Forum - 03-16-2008

[eluser]Dan Decker[/eluser]
OK, I've removed closing php tags, passed TRUE to the Model as it is called, and verified that output compression is off, still just a blank page. Is there anything else I can try? Is it possible that I got a corrupted DL of CI?

I'll try downloading again and moving my files into the new install. I'm at my wits end. As a question to the group here, does the code above look like it would even work?

TIA,


Trouble with Models - El Forum - 03-16-2008

[eluser]xwero[/eluser]
the closing tag can be removed not the begin tag.


Trouble with Models - El Forum - 03-16-2008

[eluser]Matthew Lanham[/eluser]
You have 2 views, are they both available:

$this->load->view('tickets');
$this->load->view('error_message');


Trouble with Models - El Forum - 03-16-2008

[eluser]Dan Decker[/eluser]
Yeah, I figured that out the hard way, ;-) Still having no luck.

EDIT: The php tag thing...