2012-02-29 108 views
-2

我尝试使用函数但面临问题。我在网上做研究,但没有解决方案调用一个非对象的成员函数kayitmodel()

我有一个模型。你可以看到如下:

<?php 

class kayitmodel extends CI_Model { 

    function User_model() { 
     parent::Model(); 
    } 

    function uyeEkle($username, $email, $password, $activationCode) { 
     $sha1_password = sha1($password); 

     $query = "insert into pasaj_register(username,email,password,activationCode) values(?,?,?,?)"; 
     $this->db->query($query, array($username, $email, $sha1_password, $activationCode)); 
    } 

    function uyeOnay($registrationCode) { 
     $query = "SELECT id FROM pasaj_register where activationCode = '" . $registrationCode . "' and active != 1"; 
     $result = $this->db->query($query, $registrationCode); 

     if ($result->num_rows() == 1) { 
      $query = "UPDATE pasaj_register SET active = 1 WHERE activationCode = ?"; 
      $this->db->query($query, $registrationCode); 

      return true; 
     } else { 
      return false; 
     } 
    } 

    function girisKontrol($username, $password) { 
     $sha1_password = sha1($password); 
     $query = "SELECT id FROM pasaj_register WHERE username = ? and password = ?"; 

     $result = $this->db->query($query, array($username, $sha1_password)); 

     if ($result->num_rows() == 1) 
      return $result->row(0)->id; 
     else 
      return false; 
    } 



} 

在giris控制器我使用girisKontrol功能

<?php 

class giris extends CI_Controller { 

    public function __construct() { 
     parent::__construct(); 
    } 

    public function index() { 
     $this->load->model('kayit/kayitmodel'); 
     $this->load->view('giris/giris'); 
    } 


    public function main_page() { 

     extract($_POST); 


     $userID = $this->giris->kayitmodel($username, $password); 


     if(!userID) 
      echo "yok"; 
     else 
      echo "var"; 
    } 

} 

?> 

但是当处理页面提示错误:

Fatal error: Call to a member function kayitmodel() on a non-object in C:\xampp\htdocs\pasaj\application\controllers\giris.php on line 20 

为什么呢?

+2

的[调用一个成员功能的非对象上]的整个相关部分 – Gordon 2012-02-29 18:58:07

+0

可能重复的(可能重复http://stackoverflow.com/questions/54566/call-to-a-member-函数非对象) – 2012-02-29 19:14:26

回答

2
$userID = $this->giris->kayitmodel($username, $password); 

这是错误的。 giris是你的控制器,它目前是$thiskayitmodel是你的模型。然后你需要在模型上调用一个函数。

$userID = $this->kayitmodel->girisKontrol($username, $password); 

而且在你的模型:

​​

应该是:

public function __construct() { 
    parent::__construct(); 
} 

编辑:模型需要以大写字母开始,其余小写。此外,文件名应该是类名,但全部为小写。
手册:http://codeigniter.com/user_guide/general/models.html

这应该在一个名为kayitmodel.php(注意小写'k')的文件中。

class Kayitmodel extends CI_Model { // Note the capital 'K' 

您的电话应改为:

$userID = $this->Kayitmodel->girisKontrol($username, $password); // Note the capital 'K' 

EDIT2:您的控制器应以大写字母开始了。
手册:http://codeigniter.com/user_guide/general/controllers.html

class Giris extends CI_Controller { // Note the capital 'G' 

EDIT3:你需要加载模型在控制器的构造函数,所以里面的所有方法都可以使用。

class Giris extends CI_Controller { 

    public function __construct() { 
     parent::__construct(); // Make sure this is the 1st line in the constructor 
     $this->load->model('kayit/kayitmodel'); 
    } 
+0

真的,不需要'函数User_model()'或手动'parent :: __ construct()'。这段代码很混乱。 '提取物($ _ POST);'也是可怕的,什么都不做。 – 2012-02-29 19:06:12

+0

@Madmartigan:看起来'function User_model()'在那里,因为他从另一个函数复制和粘贴,并没有改变它。 'parent :: __ construct()'是必须的,你需要在模型的构造函数中调用'CI_Model'构造函数。 – 2012-02-29 19:07:58

+0

不,您不必手动调用它,父构造函数总是运行,并且所有CI_Model构造函数都会记录它加载的调试消息。如果你重载了contstructor,你必须调用它,但如果你不这样做,则不会。 – 2012-02-29 19:09:08

相关问题