Answer the question
In order to leave comments, you need to log in
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,
]);
}
<?= ListView::widget([
'dataProvider' => $dataProvider,
'itemOptions' => ['class' => 'item'],
'itemView' => '_list_item',
'viewParams' => [
'exist' => $exist
],
]) ?>
<?php
echo $exist;
echo $model->title;
?>
Answer the question
In order to leave comments, you need to log in
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).
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 questionAsk a Question
731 491 924 answers to any question