duan3601
duan3601
2014-10-26 17:23
浏览 41
已采纳

在控制器中调用多个模型

Hello I am having trouble running more than one model in my controller.

The first model is encryption then the second model is insertion.

public function addStore()
{
    $name = $_POST['name'];
    $address = $_POST['address'];
    $gpsAddress = $_POST['gps_address'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $status = 1;

    $this->load->model('EncryptionModel');
    $password = $this->EncryptionModel->encryptPassword($password);

    $this->load->model('StoresModel');
    $this->StoresModel->addStore($name, $address, $gpsAddress, $phone, $email, $password, $status);
}

The following is the error

Fatal error: Call to a member function addStore() on a non-object in \application\controllers\stores.php

This does not occur when the encryption model call is taken out.

Encryption Model as requested

class EncryptionModel extends CI_Controller {

public function encryptPassword($password)
{
    $options = ['cost' => 12];
    $password = password_hash($password, PASSWORD_BCRYPT, $options)."
";
    return $password;
}
}

图片转代码服务由CSDN问答提供 功能建议

您好我在控制器中运行多个模型时遇到问题。

第一个模型是加密,然后第二个模型是插入。

  public function addStore()
 {
 $ name = $ _POST ['name']; 
  $ address = $ _POST ['address']; 
 $ gpsAddress = $ _POST ['gps_address']; 
 $ phone = $ _POST ['phone']; 
 $ email = $ _POST ['email'];  
 $ password = $ _POST ['password']; 
 $ status = 1; 
 
 $ this-> load-> model('EncryptionModel'); 
 $ password = $ this->  EncryptionModel-> encryptPassword($ password); 
 
 $ this-> load-> model('StoresModel'); 
 $ this-> StoresModel-> addStore($ name,$ address,$  gpsAddress,$ phone,$ email,$ password,$ status); 
} 
   
 
 

以下是错误

致命错误:在\ application \ controllers \ stores.php中的非对象上调用成员函数addStore()

当 取消加密模型调用。< / p>

请求的加密模型

 类EncryptionModel扩展CI_Controller {
 
公共函数encryptPassword  ($ password)
 {
 $ options = ['cost'=&gt;  12]; 
 $ password = password_hash($ password,PASSWORD_BCRYPT,$ options)。“
”; 
返回$ password; 
} 
} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duangai1368
    duangai1368 2014-10-26 17:57
    已采纳

    Should be...

    class EncryptionModel extends CI_Model {
    

    Note CI_Model in place of where you had CI_Controller.


    Also see the CodeIgniter class naming recommendations regarding upper and lower case best practices...

    "Class names should always start with an uppercase letter. Multiple words should be separated with an underscore, and not CamelCased. All other class methods should be entirely lowercased and named to clearly indicate their function, preferably including a verb. Try to avoid overly long and verbose names."

     

    INCORRECT:
    class superclass
    class SuperClass

    CORRECT:
    class Super_class

    class Super_class {
        function __construct()
        {
    
        }
    }
    
    点赞 评论
  • douzhui1972
    douzhui1972 2014-10-26 18:03

    Encryption Model was extending CI_Controller where it should be extending CI_Model

    Thanks everyone.

    点赞 评论

相关推荐