0

I'm currently trying to send an email to multiple employees if a condition is met; the list of users is gotten as expected but when I try to foreach the array I always get this "You cannot serialize or unserialize PDO instances" error.

this is the function I currently have to do it

function mailUsers() 
{
    $date = '03-07'; //date('m-d');
    $employee = $this->Employees->find()->where(['birthday LIKE' => '%' . $date . '%'])->toArray();
    var_dump($employee);

    foreach ($employee as $value) {
        $email = new Email();
        try {
            $email->reset();
            $email->from(['com@corp.co' => 'corp'])
                ->profile('SendGrid')
                ->to([$value['email'] => $value['full_name']])
                ->subject("test")
                ->emailFormat("both")
                ->template('default')
                ->send();
            $this->Flash->success("Success.");
        } catch (Exception $ex) {
            echo 'Exception : ', $ex->getMessage(), "\n";
        }
    }
}

Any help will be much appreciated!

DanielHolguin
  • 305
  • 4
  • 13
  • What are the values of `$employee`? If you remove `try-catch` block - does error exist? – u_mulder Mar 02 '16 at 19:57
  • @u_mulder $employee has id, name, email, birthday; the error keeps happening because the function doesn't go that far, as soon as it begins the foreach the exception is thrown. – DanielHolguin Mar 02 '16 at 20:06
  • So - if you remove `foreach` there's no error at all? – u_mulder Mar 02 '16 at 20:09
  • @u_mulder yep if I remove it, and var_dump the $employee variable it shows me all the data I need. but the foreach breaks everything – DanielHolguin Mar 02 '16 at 20:39

0 Answers0