猿问

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

设置是这样的:


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


现在由于特定位置的不可避免的原因需要取消预订。所以我添加了一个site_closure带有location_name,start_date和的表end_date,并在控制器中更新 order_itemstatus与cancelled发布的日期的所有预订site_closure


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


所以在我的 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;


            }


如果我迭代\Yii::$app->mailer->compose()in foreach,它将为每个类发送邮件,而我希望在单个邮件中取消该用户的所有类。


米琪卡哇伊
浏览 97回答 1
1回答

翻翻过去那场雪

您可以将客户的所有时间保存在一个字符串中,一个列表中,然后在电子邮件中使用它foreach( $client_emails as&nbsp; $client_email){&nbsp; &nbsp; $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();&nbsp; &nbsp; $body = "regarding cancellation&nbsp;&nbsp; &nbsp; <ul>"; // create the ul element&nbsp; &nbsp; foreach($cancelled_items as $cancelled_item){&nbsp; &nbsp; &nbsp; &nbsp; $start_time = $cancelled_item->start_time;&nbsp; &nbsp; &nbsp; &nbsp; $body .= "<li>". $start_time."</li>"; // add the li with the start_time content inside&nbsp; &nbsp; }&nbsp; &nbsp; $body .= "</ul>"; // close the list&nbsp; &nbsp; \Yii::$app->mailer->compose()&nbsp; &nbsp; ->setFrom([$from_email => $from_name])&nbsp; &nbsp; ->setTo($client_email['email'])&nbsp; &nbsp; ->setSubject('Regarding Cancellation of Classes')&nbsp; &nbsp; ->setHtmlBody($body)&nbsp; &nbsp; ->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();
随时随地看视频慕课网APP
我要回答