将 Code Igniter 与 mysql 实例一起使用,我希望能够在添加特定记录时生成电子邮件。应为其生成电子邮件的电子邮件地址和事件存储在数据库中。我认为生成这些电子邮件的代码属于 after_add 钩子,如下所示:
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();
}
但是,当我运行它时,在将记录插入索赔表时出现错误。而不是输入到表单的值,而是尝试在那里输入默认值:
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\')');
在我看来,CI 在将新记录添加到表后实际上并没有执行 after_add 钩子,因为注释掉 after_add 钩子中的所有查询并取消注释 //$recipients="thing1@gmail.com , thing2@ gmail.com"; 成功将电子邮件发送给两个收件人。所以,这不是我的电子邮件配置的问题,我使用 sendmail 成功发送了电子邮件,并设置了我的电子邮件配置文件。
添加记录后,这个钩子真的没有执行吗?如果是这样,在添加记录功能后如何实现电子邮件?