如何将Model类实例化为另一个在codeigniter中具有参数的Model类

时间:2014-01-25 15:59:21

标签: php codeigniter

如何将Model类实例化为另一个在codeigniter中具有参数的Model类。

帐户类:

    class Account extends CI_Model 
    {
         $username;
         $password;
         public function __construct($username,$password);
         {
            parent::__construct();
            $this->username=$username;
            $this->password=$password;
         }

         function login()
         {
            $result=$this->db->query("SELECT username FROM account where username='$this->$username' and password='$this->password'");
            return $result->row(1);
         }
    }

TestAccount类:

 class TestAccount extends CI_Model 
 {
        function __construct()
        {
                parent::__construct();
                $this->load->library('unit_test');
                $this->load->model('account','',TRUE);
        }

        /* In this test, the valid values of the credentials are the following:
         * Username: myUsername
         * Password: myPassword
         */

        function testCorrectCredentials()
        {
            $anAccount=new $account("myUsername","myPassword");
            $result = $anAccount->login();
            $test_res = ($result > 0); // matches content, the return value is the id
            $expected_result = true;
            $test_name = "Given the correct credentials(username and password exists in database), this function will be able to retrieve the league manager username";
            $this->unit->run($test_res, $expected_result, $test_name); 
        }
}

我想实例化我的Account类,但是如何?

1 个答案:

答案 0 :(得分:0)

当您加载模型时,它所在的文件会被自动包含在内,这意味着您不必单独访问它,在您的情况下,您可以简单地使用$ account

$anAccount = new Account("myUsername","myPassword");