Question

Using Code Igniter with a mysql instance, I would like to be able to have emails generated when specific records are added. The email addresses and events for which emails should be generated are stored in the database. I thought that the code for generating these emails belonged in an after_add hook, like so:

function c_claims_after_add( $tableModel ) {
    $CI = &get_instance();

    $CI->db->select("ee.location_specific as sp");
    $CI->db->select("u.EMail as email");
    $CI->db->select("u.idLocation as idLocation");
    $CI->db->from("emails em");
    $CI->db->join("email_events ee" , "ee.id = em.idEvent" , "LEFT");
    $CI->db->join("users u" , "u.id = em.idUser" , "LEFT");
    $CI->db->where("ee.event='Claim Added'");
    $query = $CI->db->get();

    $recipients="";
    foreach ($query->result_array() as $r) {
        if($r['sp']==1 && $r['idLocation'] != $tableModel->fields['idLocation']->dbValue)
        {
            continue;
        }
        else
        {
            $recipients .= $r['email']." , ";
        }
    }
    //$recipients="thing1@gmail.com , thing2@gmail.com";

    $CI->load->library('email');


    $CI->email->from("codeiginter@ci", "Code Igniter App");
    $CI->email->reply_to("thing1@gmail.com", "Code Guy");
    $CI->email->to($recipients);

    $CI->email->subject("Added claim for ".$tableModel->fields['ClaimNumber']->dbValue );
    $CI->email->message("This was installed on:  ".$tableModel->fields['InstallDate']->dbValue."\r\nDamage statement:  ".$tableModel->fields['DamageDescription']->dbValue);

    $CI->email->send();
    }

However, when I run this I get an error when inserting a record into the claims table. Instead of the values input to the form, the default values are trying to be input there:

clearFormError('claims_add_form'); setFormError('claims_add_form' , 'Cannot add or update a child row: a foreign key constraint fails (`codeigniter_app`.`claims`, CONSTRAINT `claims_fk3` FOREIGN KEY (`idLocation`) REFERENCES `locations` (`id`))
INSERT INTO `claims` (`Date`, `ClaimNumber`, `idStatus`, `ticket`, `idDamageType`,     `ClientName`, `InstallDate`, `DamageDescription`, `Electric`, `Hot`, `SubmittedIns`,     `idDriver`, `idHelper`, `idSecondaryHelper`, `idLocation`, `needDriverStatement`, `driverStatement`, `customerStatement`, `Notes`, `idCreatedBy`) 
VALUES (0, 0, 0, 0, NULL, 0, 0, 0, \'0\', \'0\', \'0\', NULL, NULL, NULL, 0, \'0\', NULL, NULL, NULL, \'236\')');

It appears to me that CI is not actually doing the after_add hook after it adds the new record to the table, because commenting out all of the queries in the after_add hook and uncommenting //$recipients="thing1@gmail.com , thing2@gmail.com"; successfully sends the email to both recipients. So, this is not an issue with my email configuration, I am successfully sending emails out using sendmail, and have set up my email config file.

Is this hook really not executed after the adding of the record? If so, how could I implement the email after a record is added functionality?

Was it helpful?

Solution

Making the query with $CI->db->query instead of using the selects and wheres made it so that record was inserted before the query to lookup emails was done first. Using $CI->db->select did not work even if $CI->db->flush_cache() was ran before it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top