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

Php Yii2-Swiftmailer-为每个用户添加多个动态记录,php,yii2,swiftmailer,Php,Yii2,Swiftmailer,设置如下所示: 我有预订课程的用户/客户。课程可以是每个日期、每个地点和每个讲师的多个课程 现在,由于特定位置不可避免的原因,需要取消预订。因此,我添加了一个表site\u closure,其中包含location\u name、start\u date和end\u date,并且在控制器中,将订单项目状态更新为已取消,与站点关闭 下一步是我需要向用户/客户发送邮件,详细说明取消的类的列表 因此,在我的站点中,我添加了以下代码: public function actionCreate()

设置如下所示:

我有预订课程的用户/客户。课程可以是每个日期、每个地点和每个讲师的多个课程

现在,由于特定位置不可避免的原因,需要取消预订。因此,我添加了一个表
site\u closure
,其中包含
location\u name
start\u date
end\u date
,并且在控制器中,将订单项目
状态
更新为
已取消
,与
站点关闭

下一步是我需要向用户/客户发送邮件,详细说明取消的类的列表

因此,在我的站点中,我添加了以下代码:

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,
        ]);
    }

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

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

foreach($client\u电子邮件作为$client\u电子邮件){
$cancelled\u items=OrderItem::find()->where(['location\u id'=>$model->location\u name]和['user\u id'=>$client\u email['id']])->和where(['between',date',$model->from\u date,$model->to\u date])->全部();
$body=“关于取消
    “;//创建ul元素 foreach($cancelled\u items作为$cancelled\u item){ $start\u time=$cancelled\u item->start\u time; $body.=“
  • ”$start\u time.
  • “;//添加包含start\u time内容的li } $body.=“
”;//关闭列表 \Yii::$app->mailer->compose() ->setFrom([$from\u email=>$from\u name]) ->setTo($client_email['email'])) ->setSubject('关于取消课程') ->setHtmlBody($body) ->send(); }
不要获取取消状态未设置为已取消的用户。像这样编辑
$orders
变量

$order\u items=OrderItem::find()->其中(['location\u id'=>$model->location\u name])
->andwhere(['between','date',''.$model->from\u date,$model->to\u date])
->其中(“取消状态=:取消状态”,数组(':cancellation\u status'=>'cancelled'))->all();

为什么不在forEach中使用
$client\u email['id']
来获取有关已取消课程的详细信息并向他们发送电子邮件?您是否正在尝试向
$client\u email
中存在任何
$cancelled\u项目的所有用户发送一封电子邮件?,除此之外,为什么您要使用s单独的表来取消订单,您不能在
orderItem
表中使用
tinyint
enum
列类型来管理它吗。因此,他将收到一封邮件,与您的课程一样,时间为2019年7月24日上午9点10分、2019年7月24日上午9点30分和2019年7月24日上午9点50分。像wise一样,我在7月23日至7月27日关闭了一个网站,因此如果该用户在该日期有更多的课程,则该邮件中将包含这些课程。@weegee-添加了栏目名称谢谢,看起来这就是我需要的。让我试试。这将向所有用户发送电子邮件,无论他们是否取消了课程not@weegee-是的,邮件内容的格式与预期一致,但我想我在代码中遗漏了一些其他要点,包括额外的类,然后是取消的类。嗨,weegee-刚刚更新了,但运气不好,我仍然得到5级而不是3级。请看你是否能找到其他问题,我会找到的。但是我发布问题的主要问题已经解决了,因此我接受你的回答。@Joshi try
$order\u items=OrderItem::find()->where(“location\u id=:location\u id”,array(':location\u id'=>$model->location\u name))->和where(“取消状态=:取消状态”,array(':cancellation\u status'=>'cancelled')->和where(['between'、'date'、''$model->from_date、$model->to_date])->all();
                $body.="<tr><td>".$cancelled_item->date  ."</td><td>". $cancelled_item->start_time."</td></tr>";

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