我知道这很简单,但我无法解决这个问题。请仔细研究。
我有一个名为notification_updates
的表,它的数组是这样的:
Array
(
[0] => common\models\NotificationUpdates Object
(
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 1
[title] => This is the notification to inform you about this.
[status] => 1
[created_at] => 2017-11-20 08:29:21
)
)
[1] => common\models\NotificationUpdates Object
(
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 2
[title] => This is the notification to inform you about this cricket match
[status] => 1
[created_at] => 2017-11-20 06:24:09
)
)
[2] => common\models\NotificationUpdates Object
(
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 3
[title] => Inform you about this cricket match
[status] => 1
[created_at] => 2017-11-21 11:40:31
)
)
)
现在我还有一个表格,其中第一个表格的primary_key
(id
)在表notification_id
中被称为deleted_nofitication
。
这个表也有这样的数组:
Array
(
[0] => common\models\DeletedNofitication Object
(
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[notification_id] => 1
)
)
[1] => common\models\DeletedNofitication Object
(
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[notification_id] => 2
)
)
)
现在我必须检查天气notification_updates表是否具有针对该user_id
的此值。如果它在那里,则不应该在JSON
下显示该通知。
我在PHP
(YII2
)中做过这样的事情 - 请不要在此进行比较,请检查
$notifications = NotificationUpdates::find()->where([])->all();
$outt = [];
foreach($notifications as $notification) {
$deleted_notification = DeletedNofitication::find()
->select('notification_id')
->where(['user_id'=>$user_id])
->all();
$outt[] = [
'id' => $notification->id,
'notification_title' => $notification->title
];
}
$out = [
'notification'=> $outt,
'success' => true,
'message' => 'All Notification Updates'
];
答案 0 :(得分:1)
修改强>
好吧,既然我得到了你想要做的事情,我可能会提供帮助。我对YII2感到不舒服(即我从未使用过它),但你的代码可能看起来像这样。底线是我希望SQL只返回相关记录,这样我们就不必用我们的php做这个逻辑了:
<?php
// We're trying to select all notifications except the ones that have already been shown.
// Following query uses subquery, join would be better performancewise.
// $notificationsSQL = "SELECT id,title FROM NotificationUpdates WHERE id NOT in (SELECT id FROM DeletedNotifications WHERE user_id = $user_id)";
$notificationsAlreadyShown = DeletedNofitication::find()->where(['user_id' => $user_id]);
$notifications = NotificationUpdates::find()->where(['not in', 'id', $notificationsAlreadyShown]);
if ($notifications->count() === 0){ //Or whatever method you use to count the results.
return; // Or whatever you need to do to handle this case.
}
$outt = [];
foreach ($notifications->all() as $notification) {
$outt[] = [
'id' => $notification->id,
'notification_title' => $notification->title
];
}
$out = [
'notification' => $outt,
'success' => true,
'message' => 'All Notification Updates'
];
P.s我在删除的通知上看不到列user_id
,可能需要检查。
OLD ANSWER:
对不起,您的疑问对我来说并不是很清楚。即我没有得到你想要做的事。是否仅显示尚未删除的通知?
首先引起我注意的是,您在第二次查询的where-clause中列user_id
,但我不会在表结构。我对YII2不太熟悉,但你是否想尝试做一些事情:
<?php
$notifications = NotificationUpdates::find()->where([])->all();
$outt = [];
foreach ($notifications as $notification) {
$deleted_notification = DeletedNofitication::find()
->select('notification_id')
->where(['notification_id' => $notification->id])
->all();
// If the notification has been deleted, skip to next iteration.
if (count($deleted_notification) > 0){
continue;
}
$outt[] = [
'id' => $notification->id,
'notification_title' => $notification->title
];
}
$out = [
'notification' => $outt,
'success' => true,
'message' => 'All Notification Updates'
];
虽然这是您尝试做的事情,但您应该返回查询构建器并仅选择未删除的通知。或者甚至更好,使用YII2s softdeletes(如果有的话)。
答案 1 :(得分:0)
此查询执行了我需要的所有内容..
$sql = "select * from notification_updates where NOT EXISTS
(select * from deleted_nofitication where notification_id = notification_updates.id AND user_id = ".$user_id." )" ;
$command = Yii::$app->db->createCommand($sql);
$notifications = $command->queryAll();
从$ notifications中获取值并将其添加到json。