Yii2 - Swiftmailer - 为每个用户添加多个动态记录

Yii2 - Swiftmailer - adding multiple dynamic records per user

设置是这样的:

我有 users/clients 预订了 class。 类 每个日期、每个地点和每个讲师可以有多个 class。

现在由于某些不可避免的原因需要取消预订 location.So 我添加了 table site_closure with location_name, start_date and end_date,并在控制器中将 order_item status 更新为 cancelled 针对 site_closure

中发布日期的所有预订

下一步是我需要将邮件发送到 user/client,详细说明已取消的 class 列表。

所以在我的 site_closure 控制器创建操作中我添加了这段代码:

public function actionCreate()
    {
        $model = new SiteClosure();
        $st = Yii::$app->getTable;
        if ($model->load(Yii::$app->request->post()) ) {
            $order_items= OrderItem::find()->where(['location_id'=>$model->location_name])->andwhere(['between', 'date', $model->from_date, $model->to_date ])->all();
            $users_id_array = OrderItem::find()->select('distinct(user_id)')->where(['location_id'=>$model->location_name])->andwhere(['between', 'date', $model->from_date, $model->to_date ])->asArray()->all();
            $users_id = array_values($users_id_array[0]);
            $users=implode(',',$users_id);
            $client_emails = User::find()->where(['in','user.id' ,$users])->asArray()->all();

           // var_dump($client_emails);exit;

            foreach($order_items as $order_item){
                $order_item->cancellation_status="cancelled";

                $order_item->save();
               // $user_id.=$order_item->user_id;

            }
            $model->save();

            $from_email = $st->settings('email', 'from_email');
            $from_name = $st->settings('email', 'from_name');


            foreach( $client_emails as  $client_email){
                $cancelled_items=OrderItem::find()->where(['location_id'=>$model->location_name] and ['user_id'=>$client_email['id']])->andwhere(['between', 'date', $model->from_date, $model->to_date ])->all();
                $start_time='';
                foreach($cancelled_items as $cancelled_item){
                    $cancelled_item->date;
                    $start_time.=$cancelled_item->start_time;

                }
            \Yii::$app->mailer->compose()
                ->setFrom([$from_email => $from_name])
                ->setTo($client_email['email'])
                ->setSubject('Regarding Cancellation of Classes')
                ->setHtmlBody(
                'regarding cancellation '.$cancelled_item->date .'<br>'.
                $start_time                
                )
                ->send();

            }          


            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

如果我在 foreach 中重复 \Yii::$app->mailer->compose(),它将为每个 class 发送邮件,而我希望在单个邮件中为该用户取消所有 classes .

更新: 虽然 @weegee 解决方案按预期工作,但我想我在我的代码中遗漏了一些东西,正如您在所附图片中看到的那样,我取消了 3 classes。

但是发送的邮件是针对 7 月 22 日图片中的所有 classes。 喜欢: 关于取消 日期开始时间 2019-07-22 09:50:00 2019-07-22 09:00:00 2019-07-22 09:20:00 2019-07-22 10:00:00 2019-07-22 10:10:00

我的更新代码如下所示:

foreach( $client_emails 作为 $client_email){ $cancelled_items=OrderItem::find()->where(['location_id'=>$model->location_name] and ['user_id'=>$client_email ['id'] 和 ['cancellation_status'=>'cancelled']])->andwhere(['between', 'date', $model->from_date , $model->to_date ])->all(); $body = "regarding cancellation"; $body .= "DateStart Time"; foreach($cancelled_items 作为 $cancelled_item){

                $body.="<tr><td>".$cancelled_item->date  ."</td><td>". $cancelled_item->start_time."</td></tr>";

            }
            $body.="</table>";

但它仍然包括 7 月 22 日的所有 classes。

您可以将客户的所有时间保存在一个字符串中,一个列表中,然后在电子邮件中使用它

foreach( $client_emails as  $client_email){
    $cancelled_items=OrderItem::find()->where(['location_id'=>$model->location_name] and ['user_id'=>$client_email['id']])->andwhere(['between', 'date', $model->from_date, $model->to_date ])->all();
    $body = "regarding cancellation 
    <ul>"; // create the ul element
    foreach($cancelled_items as $cancelled_item){
        $start_time = $cancelled_item->start_time;
        $body .= "<li>". $start_time."</li>"; // add the li with the start_time content inside
    }
    $body .= "</ul>"; // close the list
    \Yii::$app->mailer->compose()
    ->setFrom([$from_email => $from_name])
    ->setTo($client_email['email'])
    ->setSubject('Regarding Cancellation of Classes')
    ->setHtmlBody($body)
    ->send();
}

不获取取消状态未设置为取消的用户。像这样编辑你的 $orders 变量

$order_items = OrderItem::find()->where(['location_id'=>$model->location_name])
->andwhere(['between', 'date', '', $model->from_date, $model->to_date ])
->andWhere("cancellation_status=:cancellation_status",array(':cancellation_status'=>'cancelled'))->all();