很抱歉,我有一个功能在我的文章中添加了回答问题的答案,所以当用户添加问题的答案时,系统会向写下此问题的用户发送电子邮件。这是我在控制器中的功能:
public function add($question_id = null) {
if ($this->request->is('post')) {
$this->loadModel('Question');
$this->Answer->set(array('question_id'=>$question_id));
$this->Answer->create();
$user = $this->Question->find('all',array('fields' => 'Useri.email',
'joins' => array(array('conditions' => array('Useri.id = Question.user_id'),
'table' => 'users',
'alias' => 'Useri',
'type' => 'INNER'))));
$email = new CakeEmail();
$email->config('server');
$email->template('answer');
$email->viewVars(array(
'to'=>$user['User']['email']
));
$email->to($user);
$email->from(array('gais@wahanaartha.com' => 'IT Sharing Knowledge Team'));
$email->subject('Sharing Knowledge Notification');
$result=$email->send();
if ($this->Answer->save($this->request->data)) {
$this->Question->updateAll(array('Question.status' => '1'),
array('Question.id' => $question_id));
$this->Session->setFlash(__('The answer has been saved.'));
return $this->redirect(array('controller' => 'questions', 'action' => 'view', $question_id));
} else {
$this->Session->setFlash(__('The answer could not be saved. Please, try again.'));
return $this->redirect(array('controller' => 'questions', 'action' => 'view', $question_id));
}
}
$questions = $this->Answer->Question->find('list');
$users = $this->Answer->User->find('list');
$this->set(compact('questions', 'users', 'tests'));
}
<?php
App :: uses('AppModel','Model');
class Question扩展了AppModel {
public $validate = array(
'topic_id' => array(
'rule' => 'numeric',
'required' => true,
'message' => 'Topic must be choosen !'
),
'user_id' => array(
'rule' => 'numeric',
'required' => true,
'message' => 'User must be choosen !'
),
'question' => array(
'minLength' => array(
'rule' => array('minLength', 12),
'required' => true,
'message' => 'Question min. 12 characters !'
),
'unique' => array(
'rule' => array('checkUnique', 'question'),
'message' => 'Question don\'t be same !'
)
),
'description' => array(
'minLength' => array(
'rule' => array('minLength', 12),
'required' => true,
'message' => 'Description min. 12 characters !'
),
'unique' => array(
'rule' => array('checkUnique', 'description'),
'message' => 'Description don\'t be same !'
)
)
);
public $belongsTo = array(
'Topic' => array(
'className' => 'Topic',
'foreignKey' => 'topic_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
public $hasMany = array(
'Answer' => array(
'className' => 'Answer',
'foreignKey' => 'question_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
function checkUnique($data, $fieldName) {
$valid = false;
if(isset($fieldName) && $this->hasField($fieldName)) {
$valid = $this->isUnique(array($fieldName => $data));
}
return $valid;
}
}
但是当运行此代码时,出现错误“无效的电子邮件'数组'”,我已经检查了我的查询,当我在navicat中运行我的查询很好,任何帮助将不胜感激 感谢
答案 0 :(得分:2)
与CakeEmail合作:
**to( ) public
To
Parameters
mixed $email optional null
Null to get, String with email, Array with email as key, name as value or email as value (without name)
string $name optional null
Returns
mixed
mixed**
所以你的$ user数组必须如下:[&#34; email1@mail.com" =&gt;&#34; Name User&#34;,&#34; email2@mail.com" = &gt;&#34;名称User2&#34;] 或字符串:&#34; email@mail.com"
试试这个!并告诉我更多关于。
(try to set $email->to(array('mail@mail.com'=>'Name User') );
如果不起作用,请告诉我们有关您的错误的更多信息