dongtou9934
2012-08-27 17:02
浏览 59
已采纳

为什么CodeIgniter在模型I中创建了“调用未定义的方法”?

So I've been tasked with creating a function for our member database that will alert someone on a monthly basis as to when their membership is going to expire. We send out one email if a person is 6 months or less from their membership expiration, and another if they are 3 months out. No big whoop. I first create a model like this:

class Email extends CI_Model {

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

// -----------------EMAIL-----------------
function emailMessage($message = 1,$recipient = 1777) { // Default "Test" Numbers
    $rInfo = $this->db->query("SELECT * FROM Members WHERE intKey = " . $recipient)->row();
    $eInfo = $this->db->query("SELECT * FROM Emails WHERE intKey = " . $message)->row(); 

    // Replacements?
    $bodyCopy = $eInfo->txtEmail;
    $bodyCopy = str_replace("[firstname]",$rInfo->strFirstName,$bodyCopy);  

    $this->load->library('email');
    $this->email->from('someone@example.com','John Q Public');
    $this->email->to($rInfo->strEmail); 
    $this->email->subject($eInfo->strTitle);
    $this->email->message($bodyCopy);
    $this->email->send();
    $this->email->clear();
}
}

All fine and good, and when I call this model from any other part of the site I have built, it works fine. It's a great way to handle any stock emails that have to be sent out to our constituency. Normally, it's just something like this within the code:

$this->load->model('email');
$this->email->emailMessage(8,$this->session->userdata('memberKey'));

Works like a charm. The big difference between where I've used it previously, and where I use it now is that I'm enclosing the call to the model within a loop as follows:

public function warningEmails() {
    $this->load->model('email');
    $sql = 'SELECT
                *, FLOOR(
                    DATEDIFF(dtAccreditationEnd, now())/ 30
                )AS diff
            FROM
                tblMembers
            WHERE
                enmAccredited = "yes"
            AND dtAccreditationEnd < DATE_ADD(now(), INTERVAL 6 MONTH)
            ORDER BY
                diff
    ';  
    $emailSend = $this->db->query($sql);
    foreach ($emailSend->result() as $row) {
        if ($row->diff <= 3) {
            $letter = 10;
        } else {
            $letter = 9;
        }
        $this->email->emailMessage($letter,$row->intMembersKey);
    }
}

It works great the first time through the loop, and fails with the following message the very next iteration through the loop:

Fatal error: Call to undefined method CI_Email::emailMessage() in /home/contract/public_html/members/application/controllers/staff.php on line 1033

Line 1033 is this line:

  $this->email->emailMessage($letter,$row->intMembersKey);

I there a problem where this model can only be called once in a given instance? What am I doing wrong? Thank you in advance for your assistance!

EDIT: I'm going to add a var_dump($this->email); to see what it could be...

object(CI_Email)#30 (46) { ["useragent"]=> string(11) "CodeIgniter" ["mailpath"]=> string(18) "/usr/sbin/sendmail"
["protocol"]=> string(4) "mail" ["smtp_host"]=> string(0) ""
["smtp_user"]=> string(0) "" ["smtp_pass"]=> string(0) ""
["smtp_port"]=> string(2) "25" ["smtp_timeout"]=> int(5)
["smtp_crypto"]=> string(0) "" ["wordwrap"]=> bool(true)
["wrapchars"]=> string(2) "76" ["mailtype"]=> string(4) "text"
["charset"]=> string(5) "utf-8" ["multipart"]=> string(5) "mixed" ["alt_message"]=> string(0) "" ["validate"]=>
bool(false) ["priority"]=> string(1) "3" ["newline"]=>
string(1) " " ["crlf"]=> string(1) " " ["send_multipart"]=>
bool(true) ["bcc_batch_mode"]=> bool(false) ["bcc_batch_size"]=> int(200) ["_safe_mode"]=> bool(false) ["_subject"]=> string(0) "" ["_body"]=> string(357) "Geoffrey,

This is a reminder that your PDCA Accreditation will expire in 3 months. Visit your Membership Dashboard to view all complete and incomplete course work at your convenience. The Membership Dashboard will assist you through the completion process. For more information, please contact Someone at Someone@example.com or 1-800-555-1212." ["_finalbody"]=> string(0) ""
["_alt_boundary"]=> string(0) "" ["_atc_boundary"]=> string(0) "" ["_header_str"]=> string(0) "" ["_smtp_connect"]=>
string(0) "" ["_encoding"]=> string(4) "8bit" ["_IP"]=>
bool(false) ["_smtp_auth"]=> bool(false) ["_replyto_flag"]=>
bool(false) ["_debug_msg"]=> array(0) { } ["_recipients"]=>
string(17) "recipient@example.com" ["_cc_array"]=> array(0) { } ["_bcc_array"]=> array(0) { } ["_headers"]=> array(5) { ["From"]=> string(54) ""Someone Example" " ["Return-Path"]=> string(28) "" ["Cc"]=> string(16) "copy@example.com" ["Bcc"]=> string(17) "blindcopy@example.com" ["Subject"]=> string(49) "=?utf-8?Q?PDCA_Accreditation_Expiration_Warning?=" } ["_attach_name"]=> array(0) { } ["_attach_type"]=> array(0) { } ["_attach_disp"]=> array(0) { } ["_protocols"]=> array(3) { [0]=> string(4) "mail" [1]=> string(8) "sendmail" [2]=> string(4) "smtp" } ["_base_charsets"]=> array(2) { [0]=> string(8) "us-ascii" [1]=> string(9) "iso-2022-" } ["_bit_depths"]=> array(2) { [0]=> string(4) "7bit" [1]=> string(4) "8bit" } ["_priorities"]=> array(5) { [0]=> string(11) "1 (Highest)" [1]=> string(8) "2 (High)" [2]=> string(10) "3 (Normal)" [3]=> string(7) "4 (Low)" [4]=> string(10) "5 (Lowest)" } }

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dongsaohu6429 2012-08-27 18:02
    已采纳

    Try renaming the official email library to another one by doing so,

    $this->load->library('email', NULL, 'ci_email');
    

    Then access it by doing this,

    $this->ci_email->from('someone@example.com','John Q Public');
    

    The current email library has overwritten your email model.

    已采纳该答案
    打赏 评论
  • douzhi1919 2012-08-27 17:57

    Maybe I am misreading this, but doesn't this line: $this->load->library('email');

    Load the email library in which this line is written? I think you are recursively killing yourself.

    打赏 评论
  • drfls28608 2012-11-10 11:34

    i could not get my library class MbSec{} loaded neither with autoload

    $autoload['libraries'] = array('database', 'mbsec');
    

    or a load in the controller.

    $this->load->library('mbsec');
    

    but is seems to work with Capitals, isn't that against the logic?

    $autoload['libraries'] = array('database', 'MbSec');
    
    打赏 评论
  • dongqu9917 2012-12-02 22:07

    I was having similar symptoms, but due to a different issue. I had recently changed my file names of my models from name_model.php to Name_model.php through my editor of choice, and little did I realize that my editor didn't actually remove the old name_model.php file, so two files were sitting in the directory with the same name and different case.

    I was creating a new function in my model, but it was reading the old model file, which held me up for a good few hours until I took a peek at the actual files in the directory.

    Simply removing the old files helped my issue.

    打赏 评论

相关推荐 更多相似问题