2017-11-21 4 views
2

매우 간단하지만이를 해결할 수 없습니다. 이것을 들여다보십시오.PHP에서 array의 값을 비교하십시오 - 존재하지 않으면 json 배열에 추가하십시오

나는 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가) 테이블 deleted_nofiticationnotification_id라고 foriegn 1 개 이상의 테이블이있다.

이 표는이 같은 배열이 있습니다

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

이 쿼리는이 내가 필요한 모든 것을 ..

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

그리고 $ 통지에서 값을 복용하고 JSON으로 그 추가 작업을 수행합니다.

1

편집 :

좋아, 지금은 얻을 당신이 무엇을하려는 할 수 있을지도 몰라요. YII2에 익숙하지 않습니다 (즉, 한번도 사용하지 못했습니다). 그러나 코드는 이와 비슷할 수 있습니다. , 당신의 deletednotifications에 열 user_id이 표시되지 않는

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

추신 : 최하위는 우리가 우리의 PHP와 그 논리를 할 필요가 없습니다 수 있도록 SQL 만, 관련 레코드를 반환 할 것입니다 확인해야 할 부분 일 수 있습니다.


OLD 답변 :

죄송합니다, 당신은 질문이 나에게 정확하게 명확하지 않다입니다. 나는. 나는 네가하려는 일을 못 얻는다. 삭제되지 않은 알림 만 표시됩니까?

가장 먼저 눈에 띄는 점은 이 두 번째 쿼리의 where 절에 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를 사용하는 것이 좋습니다 (해당되는 경우).

+0

deleted_nofitication ..에서 알림을 삭제했습니다. 이제 하나의 테이블 notification_updates에 모든 알림이 있습니다. 나는 거기에없는 알림을 사용자 ID와 함께 삭제하고 싶습니다.id = 1 인 경우 user_id = 1 인 deleted_nofitication에 있습니다. notification_updates 배열에서 그 통지를 얻지 않아야합니다. –

+0

@SalmanRiyaz 당신이하려는 것을 봅니다. 편집을보십시오. – user2693053