Welcome Guest, Not a member yet? Register   Sign In
Why can I use double colon to invoke model function directly in CI?
#1

[eluser]nlsdnm[/eluser]
This is so weird...I was trying to make my codes more ORM style (without an actual ORM).

Code:
class User extends CI_Model {

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

        public function search_by_email($email) {

        $this->db->from('users')->where('email', $email);
        $query = $this->db->get();
        if ($query->num_rows()==0) {
            return false;
        }else {
            return $query->result();
        }
        return null;
    }
}

search_by_email is not a static function right?

This is my controller class:

Code:
class Application extends CI_Controller {

public function __construct()
{
  parent::__construct();
  $this->load->model('User');
}

public function index()
{  
                 $user = User::search_by_email("[email protected]");

          print_r($user);
          exit;
        }

It is weird because, if I add static to my model function...the server crashes. If I don't load my model in controller's construct function, the server crashes.

Even if I create this model function:

Code:
public static function search_by_email($email) {

        User::db->from('users')->where('email', $email);
        $query = User::db->get();
        if ($query->num_rows()==0) {
            return false;
        }else {
            return $query->result();
        }
        return null;
    }

This still doesn't work at all.

Can anyone explain this phenomenon? It's not really a problem..I'm glad I can even instantiate a model in my controller class..but I'm just curious how does this happen?
#2

[eluser]Zack K.[/eluser]
I don't see why you would try and use colons as you have to call everything in codeigniter threw codeigniter...
This,
Code:
class Application extends CI_Controller {

public function __construct()
{
  parent::__construct();
  $this->load->model('User');
}

public function index()
{  
                 $user = User::search_by_email("[email protected]");

          print_r($user);
          exit;
        }

should be,
Code:
class Application extends CI_Controller {

public function __construct()
{
  parent::__construct();
  $this->load->model('User');
}

public function index()
{  
                 $user = $this->user->search_by_email("[email protected]");

          print_r($user);
          exit;
        }
#3

[eluser]nlsdnm[/eluser]
[quote author="Zack K." date="1380211543"]I don't see why you would try and use colons as you have to call everything in codeigniter threw codeigniter...
This,
Code:
class Application extends CI_Controller {

public function __construct()
{
  parent::__construct();
  $this->load->model('User');
}

public function index()
{  
                 $user = User::search_by_email("[email protected]");

          print_r($user);
          exit;
        }

should be,
Code:
class Application extends CI_Controller {

public function __construct()
{
  parent::__construct();
  $this->load->model('User');
}

public function index()
{  
                 $user = $this->user->search_by_email("[email protected]");

          print_r($user);
          exit;
        }
[/quote]

Yes...I know the standard way of doing this...but the CodeIgniter's way is quite dated right? I just want to make it more modern..even more OOP style..the better way for me to do this..I assume is to use

Code:
$userHandler = new User();
$user = $userHandler->search_by_email("");




Theme © iAndrew 2016 - Forum software by © MyBB