我试图读取电子邮件的文本文件,并尝试将这些电子邮件插入具有某些修改值的数据库表中。使用蛋糕php和mysql数据库。 这是我的代码:
控制文件:
public function addLead()
{
//$this->layout=false;
//$this->autoRender=false;
set_time_limit(100000);
$this->loadModel('Client');
$user=$this->Session->read("Auth.User");
if ($this->request->is('post')) {
$count = 0;
$upFile = $this->request->data['Lead']['filename'];
$file_ext = @strtolower(end(explode('.', $upFile['name'])));
$expensions = array("txt");
$result = "";
if (in_array($file_ext, $expensions) === false) {
$errors[] = "extension not allowed, please choose a txt file.";
}
if (empty($errors) == true) {
$myfile = fopen($upFile['tmp_name'], "r") or die("Unable to open file!");
while (!feof($myfile)) {
$lead = fgets($myfile, 4096);
$lead = preg_replace('/\s+/', '', $lead);
$pieces = explode("@", $lead);
$clientMailBox = $pieces[0];
$clientMailHost = $pieces[1];
$clientInfo = $this->Client->addClient($user['id'], $clientMailBox, $clientMailBox, $clientMailHost,$lead, "","","","");
}
fclose($myfile);
} else {
}
//$this->set('result', $elinkDec);
$this->Session->setFlash(__("<div class=\"alert alert-danger flash animated\">$count Leads Added<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">×</button></div>"));
}
}
模型功能:
public function addClient($userId=null, $clientName = null, $clientMailBox = null, $clientMailHost = null,$clientEmail=null, $emailFrom = null,$msgId=null,$msgSubject=null,$pho=null)
{
$getClient = $this->find('first',array('conditions'=>array('user_id'=>$userId,'email'=>$clientEmail,'status'=>1)));
if (empty($getClient)) {
$this->set(array('id' => null,'user_id'=>$userId, 'email_from' => $emailFrom,'phone'=>$pho, 'name' => $clientName, 'mailbox' => $clientMailBox, 'host' => $clientMailHost, 'email' => $clientEmail,'email_from'=>$emailFrom,'message_id'=>$msgId,'subject'=>$msgSubject,'response'=>1, 'status' => 1,));
$saveClient = $this->save();
return $saveClient;
} else {
if($getClient['Client']['response_status']==1){
if($pho != '0') $this->set(array('id' => $getClient['Client']['id'],'phone'=>$pho,'response'=>$getClient['Client']['response']+1, 'message_id' => $msgId,'subject' => $msgSubject, 'response_status' => 0,));
else $this->set(array('id' => $getClient['Client']['id'],'response'=>$getClient['Client']['response']+1, 'message_id' => $msgId,'subject' => $msgSubject, 'response_status' => 0,));
$saveClient = $this->save();
return $saveClient;
}
}
}
但是它的工作太慢了。...我试图插入10万封电子邮件,并且要花30多分钟才能执行。
答案 0 :(得分:0)
也许100k find(first,...)正在减慢它的速度。 您是否尝试过将递归属性设置为-1? (例如$ this-> Client-> recursive = -1;) 或尝试将find(first,condition)更改为hasAny(conditions) (hasAny使用find('count')的条件与您在find中设置的条件相同,但同时设置了recurisve属性。