windweller
windweller

Reputation: 2385

My CI model class with static method is failing

OK...first I read this article..

http://net.tutsplus.com/tutorials/php/6-codeigniter-hacks-for-the-masters/

In the first hack, it says if I include these in my config.php file:

function __autoload($class) {  
    if (file_exists(APPPATH."models/".strtolower($class).EXT)) {  
        include_once(APPPATH."models/".strtolower($class).EXT);  
    }  
}  

I don't need to extend my model class to CI_Model.

Then the I tried to use $this->db->select('email')->from('users')->where('email', $email); in my model class..CodeIgniter tells me "db" property is not found. Has anyone tried this hack before and know how to get it to work?

class User {

public $id;
public $email;
public $displayName;
public $password;

public static function search_by_email($email) {
    $user = new User;

    $user->db->select('email')->from('users')->where('email', $email);
    $query = $user->db->get();
    if ($query->num_rows()==0) {
        return false;
    }else {
        foreach ($query->result() as $key => $val) {
            $user->email = $val['email'];
            $user->displayName = $val['displayName'];
           return $user;
        }
    }
    return null;
}

When I call the static function in my controller I use:

        $user = User::search_by_email("[email protected]");

If I don't extend User, it will show a CI warning: Message: Undefined property: User::$db. If I use User extends CI_Model {}, the server will crash.

Does anyone have an idea about this situation?


Edited:

Then I dumped the silly Nettus' tutorial code, thanks to you guys...but my model is still not working:

class User extends CI_Model {

    public $id;
    public $email;
    public $displayName;
    public $password;
public function search_by_email($email) {

        $user = new User();

        $this->db->select('email')->from('users')->where('email', $email);
        $query = $this->db->get();
        if ($query->num_rows()==0) {
            return false;
        }else {
            foreach ($query->result() as $key => $val) {
                $user->email = $val['email'];
                $user->displayName = $val['displayName'];
               return $user;
            }
        }
        return null;
    }

In my controller I use:

$this->load->model('User');

$user = $this->User->search_by_email("[email protected]");

My server still crashed.....am I forbidden to write class variables like public $id in my model?

Upvotes: 1

Views: 557

Answers (1)

cg.mike
cg.mike

Reputation: 191

This part of code doesn't make any sense:

function __autoload($class) {  
   if (file_exists(APPPATH . "models/" . strtolower($class).EXT)) {  
       include_once(APPPATH . "models/" . strtolower($class).EXT);  
   }  
}  

as long as you can load your model from autoload.php under config folder or just do the following when you need the model:

$this->load->model('model_name');

then just call your function.

Upvotes: 1

Related Questions