K
K
kranopodem2016-03-04 08:47:27
Yii
kranopodem, 2016-03-04 08:47:27

How to check if a user has a bookmarked entry?

The main page displays a list of records. It is necessary to do something so that from the AchieveUser table it is checked whether this entry has been added to the database for this user (such as a bookmark that he saved this entry). At the same time, "Added" is attributed to the added articles.
I do not understand how in this code, in the WHERE condition, substitute the id of the model (achieve) so that it checks each article in the ListView. Or is it done in some other way?
CONTROLLER - AchieveController.php

public function actionIndex()
{
    $searchModel = new AchieveSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    if (AchieveUser::find()->where([
       'id_user' => Yii::$app->user->identity->id, 
       'id_achieve' => ****ЧТО СЮДА ПИСАТЬ****
       ])->exists()){
        $exist="Добавлено";
    }

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
        'exist' => $exist,
    ]);
}

VIEW - index.php
<?= ListView::widget([
    'dataProvider' => $dataProvider,
    'itemOptions' => ['class' => 'item'],
    'itemView' => '_list_item',
    'viewParams' => [
       'exist' => $exist
    ],
]) ?>

VIEW-_list_item.php
<?php
        echo $exist;
        echo $model->title;
    ?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kirill Arutyunov, 2016-03-04
@kranopodem

Get a list of id entries that the user has bookmarked.
Then, when displaying a list of records, check in_array(record_id, list_of_records_in_bookmarks).

V
Viktor, 2016-03-04
@lxShaDoWxl

Pretty simple

<?= ListView::widget([
    'dataProvider' => $dataProvider,
    'itemOptions' => ['class' => 'item'],
    'itemView' => '_list_item',
    'viewParams' => [
       'achieves' => AchieveUser::find()
            ->where([
                'id_user' => Yii::$app->user->identity->id,
            ])
            ->select('id_achieve')
            ->index('id_achieve')
            ->all()
    ],
]) ?>

<?php
        if(isset($achieves[$model->id])){
echo 'Добавлено';
}
        echo $model->title;
    ?><code >

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question