php - 在 Symfony 中传递数据数组
问题描述
我在 db 中有我的通知表,我在其中设置用户的所有读取通知。
目前,这个函数只接收一个 id,但也应该有一个选项来传递 id 数组,一次将多个标记为已读。我需要扩展这个函数,以便它处理 $this>data['id'] 是数组的情况。
我该如何解决?
我的代码:
public function readNotification(User $user, $notificationId)
{
$notification = $this->getNotificationRepository()->findOneBy([
'user' => $user,
'id' => $notificationId
]);
if($notification) {
$notification->setRead(new \DateTime());
$this->em->flush();
}
}
我的控制器:
$this->requirePostParams(['id']);
$this->get('app.service')->readNotification(
$this->data['user'],
$this->data['id']
);
return $this->success();
解决方案
在现实生活中,获取所有通知并使用flush
. 假设您的案例应该更新 1K 或更多通知。这将导致 1K 更新查询。我建议您创建一个不同的UPDATE
查询并在单个查询中更新所有相关项目:
UPDATE `tableName` SET `read` = NOW() WHERE id IN (id1, id2, ....)
id1, id2 ...
是您要更新的 id 列表。
在您的功能中,您可以执行以下操作:
public function readNotification(User $user, $notificationIds)
{
if (!is_array($notificationIds)) {
$notificationIds = [$notificationIds];
}
// other code
}
推荐阅读
- ios - UITableViewCell中UIListContentConfiguration和UIBackgroundConfiguration的基本使用?
- python - 在 Altair Plot 中单击以暂停鼠标悬停?
- python - 数据框使用新名称创建新列
- asp.net - 文件输入未定义
- android-fragments - 将值从一个片段传递到另一个片段时遇到问题
- xml - 在scala中访问xml的所有元素
- gremlin - 在 Gremlin 查询中使用通配符
- laravel - 视图刀片搜索过滤器和分页在 Laravel 中消失
- css - 在 Meteor-React-Apollo-Graphql 堆栈中使用 Stylus 导入字体
- ms-access - MS Access:组合框 - 取消选择项目以返回原始状态