将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成功发送电子邮件,并设置了我的电子邮件配置文件。
添加记录后是否真的没有执行此挂钩?如果是这样,我怎样才能在添加记录功能后实现电子邮件?
答案 0 :(得分:0)
使用$ CI-> db->查询进行查询而不是使用选择和轮询使得在首先查询查找电子邮件的查询之前插入记录。即使在它之前运行了$ CI-> db-> flush_cache(),使用$ CI-> db->选择也不起作用。