1

Sorry before, i have a function add answer for answering question in my article, so when user add answer for a question, system will sending an email to user who wrote this question. This is my function in controller :

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 extends 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;
}

}

But when run this code, there is error "Invalid Email 'Array'", i have been check my query, when i run my query in navicat is fine, any help will be greatly appreciated thanks

dery
  • 13
  • 8

1 Answers1

2

Concording with 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**

So your $user array must like : ["email1@mail.com"=>"Name User", "email2@mail.com"=>"Name User2"] or a string : "email@mail.com" .

Try this ! and tell me more about.

(try to set $email->to(array('mail@mail.com'=>'Name User') );

If isnt work , please tell us more about your bug

MouradK
  • 1,527
  • 1
  • 11
  • 14
  • Hi @MouradK thanks before, i was try your solution and running oke, but in my question i want to send that email to user who wrote a questions in article thats mean just only one user who wrote the question and i try to foreach the user email with condition and set `$email->to(array($email))` but still invalid email 'array' – dery Nov 19 '14 at 03:21
  • Is it a user logged ? use $this->Auth->user() to get your user logged, and take informations of session. Or can you past your Model Question & User to see links (belongsTo & cie ) – MouradK Nov 19 '14 at 10:12
  • is not what i mean, so the business process is when a user write the question, system will send an email to admin to give notification if there is a question from user and then admin will create an answer for this question and send email to user who wrote this question – dery Nov 20 '14 at 10:33
  • so i create the query for find user cause user have group_id 2 and admin have group_id 1 – dery Nov 20 '14 at 10:35
  • Can you post your Question Model , (it s the questionner which receive the email ). – MouradK Nov 20 '14 at 14:24