2

I know it's very simple but I'm unable to resolve this. Please look into this.

I have a table called notification_updates and it's array is like this:

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
        )
    )
)

Now I have 1 more table where primary_key (id) of first table is foriegn called notification_id in table deleted_nofitication.

This table also has array like this:

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
        ) 
    )
)

Now I have to check weather notification_updates table have this value against that user_id. If it's there then it should not that notification should not be displayed under JSON.

I have Done like this in PHP (YII2) - not doing compare in this please check

$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'
]; 

2 Answers 2

1

EDIT:

Alright, now that I get what you're trying to do, I might be able to help. I'm not comfortable with YII2 (i.e. I've never used it), but your code may look something like this. The bottomline is that I want the SQL to only return the relevant records, so that we don't have to do that logic with our 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 I don't see a column user_id on your deletednotifications, might be something to check.


OLD ANSWER:

Sorry, you're question isn't exactly clear to me. I.e. I don't get what you're trying to do. Is it only show the notifications that haven't been deleted?

The first thing that catches my eye, is that you're have a column user_id in the where-clause of your 2nd query, but I don't see it in the table structure. I'm not too familiar with YII2, but are you perhaps trying to do something along the lines of:

<?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'
]; 

Although if that's what you're trying to do, you should probably go back to your query builder and only select the notifications that aren't deleted. Or even better, use YII2s softdeletes (if it has that).

Sign up to request clarification or add additional context in comments.

1 Comment

I have deleted notification in deleted_nofitication.. now i have all notification in one table notification_updates.. I want to fetch notification which are not there in deleted_nofitication with the user_id. If id=1 is there in deleted_nofitication with user_id=1. then in notification_updates array i should not get that notification.
0

This query does this everything which i needed..

 $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();

And taking the value from $notifications and add that to json.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.