Yii2 - Swiftmailer - 为每个用户添加多个动态记录 [英] Yii2 - Swiftmailer - adding multiple dynamic records per user
问题描述
设置是这样的:
我有预订课程的用户/客户.课程可以是每个日期、每个地点和每个讲师的多个课程.
I have users/clients who book classes. Classes can be multiple classes per date and per location and per instructor.
现在由于特定位置不可避免的原因需要取消预订.所以我添加了一个表 site_closure
和 location_name
, start_date
和 end_date
,并在控制器中将 order_item status
更新为 cancelled
针对 site_closure
Now there is a need to cancel the bookings for unavoidable reasons for specific location.So I added a table site_closure
with location_name
, start_date
and end_date
, and in the controller update the order_item status
as cancelled
against all the bookings for the dates posted in site_closure
下一步是我需要向用户/客户发送邮件,详细说明取消的课程列表.
Next step is I need to send the mail to user/client detailing the list of classes cancelled.
因此在我的 site_closure 控制器创建操作中,我添加了以下代码:
so in my site_closure controller create action I have added this code:
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()
,它将为每个类发送邮件,而我希望在一封邮件中取消该用户的所有课程.
If I iterate the \Yii::$app->mailer->compose()
in foreach
, it will send mail for each class, whereas I want all classes cancelled for that user in single mail.
更新:虽然@weegee 解决方案按预期工作,但我想我在我的代码中遗漏了一些内容,正如您在附加图像中看到的那样,我取消了 3 个课程.
update: while @weegee solution is working as expected, but I think I have missed something in my code as you can see in the attached images, I have 3 classes cancelled.
但发送的邮件是针对 7 月 22 日图片中的所有课程.喜欢:关于取消日期开始时间2019-07-22 09:50:002019-07-22 09:00:002019-07-22 09:20:002019-07-22 10:00:002019-07-22 10:10:00
but the mail sent is for all the classes in the picture for 22nd July. like: regarding cancellation Date Start Time 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
我的更新代码如下所示:
my update code looks like this:
foreach( $client_emails as $client_email){$cancelled_items=OrderItem::find()->where(['location_id'=>$model->location_name] and ['user_id'=>$client_email['id'] and ['cancellation_status'=>'cancelled']])->andwhere(['between', 'date', $model->from_date, $model->to_date ])->all();$body = "关于取消";$body .= "日期开始时间";foreach($cancelled_items as $cancelled_item){
foreach( $client_emails as $client_email){ $cancelled_items=OrderItem::find()->where(['location_id'=>$model->location_name] and ['user_id'=>$client_email['id'] and ['cancellation_status'=>'cancelled']])->andwhere(['between', 'date', $model->from_date, $model->to_date ])->all(); $body = "regarding cancellation"; $body .= "DateStart Time"; foreach($cancelled_items as $cancelled_item){
$body.="<tr><td>".$cancelled_item->date ."</td><td>". $cancelled_item->start_time."</td></tr>";
}
$body.="</table>";
但它仍然包括 7 月 22 日的所有课程.
but still it is including all the classes for 22July.
推荐答案
您可以将客户的所有时间保存在字符串、列表中,然后在电子邮件中使用
You can save all the time for a client in a string, in a list and then use that in the email
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
变量
Don't fetch the users which have the cancellation status not set to canceled. Edit your $orders
variable like this
$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();
这篇关于Yii2 - Swiftmailer - 为每个用户添加多个动态记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!