Foreach只接收最后一个元素

时间:2015-05-02 07:39:06

标签: php zend-framework foreach zend-framework2 zend-form

我希望将邮件发送给阵列中的所有用户,但只发送给最后一位用户。

foreach一定有问题,但我找不到导致这种情况的原因:

public function bulkmailAction()
{
    $users    = $this->getUserTable()->fetchAll();
    $projects = array();
    foreach ($users as $usersList)
    {
        $projects[$usersList->id] = $this->getProjectTable()->fetchJoin("projectuser", "projectuser.project=project.id", "user=$usersList->id");
    }
    $this->view->projects = $projects;
    $user = new Mailing();
    $form = new BulkMailForm($user);
    $form->get("submitbutton")->setValue("Send mail");
    $this->view->form = $form;
    $form->setInputFilter($user->getInputFilter());
    $form->setData(array_merge_recursive($this->getRequest()->getPost()->toArray(),$this->getRequest()->getFiles()->toArray()));
    if ($this->getRequest()->isPost()) {
        if ($form->isValid()) {
            foreach ($users as $usersList) {
                $formData = $form->getData();

                //redirect immediately without showing a view
                $subject = $formData["subject"];
                $message = $formData["message"];
                if ($message != strip_tags($message)) {
                    $message = \BOZA\Plugins\Functions::relToAbs($message);
                }
                $user->setSent(0);
                $user->setUser($usersList->id);
                $user->setName($usersList->name);
                $user->setSurname($usersList->surname);
                $user->setDate("0000-00-00");
                $user->setEmail($usersList->email);
                $user->setSubject($subject);
                $user->setHtml(true);
                $user->setMessage($message);
                $this->getTable("Mailing")->saveMailing($user);
                $this->view->setTerminal(true);
            }
        }
        $this->cache->success = "Mailing «".$user->toString()."» was successfully saved";
        return $this->redirect()->toRoute('admin', array('controller' => 'mailing', 'action' => 'index'));
    }
    ...

1 个答案:

答案 0 :(得分:0)

您可以使用邮件将视图创建为 BulkMailForm 表单,但您可以多次设置用户,最后设置为最后一个。

邮件发送给最后设置的用户。这是标准的bevaior。您需要添加多个收件人而不是设置一个收件人和/或创建多个主要收件人,每个用户一个。

由于您在代码中使用了未定义的对象,因此很难在答案中提供更具体的指导。