Logged in users in my system can chose favorite CPV ID's. CPV's are main product of application, and they are stored in table cpv
. Some columns in cpv
table are : id
, title
, url
.
Now I need to allow users to check which CPV's they like, and store/use that. I have formed user_cpv
table that should store user and cpv id relation. Fields are id
, user_id
, cpv_id
.
So first I would display all CPV's for the logged in user, but also, I would give him check boxes next to each of them so he can chose 3 favorite ones. Like this:
Before I continue I need to ask one subquestion: how can I display checkboxes without this label pulled from my model ( Cpv ID ) ?
To continue: so in my index action of my controller, I am pulling cpv data from Cpv
model. But also I guess that I need to pull stored values from user_cpv
table, because for each user there, there can be up to 3 cpv_id
stored as his favorites. I guess that I need that information if I want to mark some of the Cpv ID
as checked. So if user has cpv_id
in user_cpv
table, those ids are marked as checked in the whole list displayed in the table that I showed you.
The image above displays the index action result ( I do not use any widgets ). I want to allow user to check some of these checkboxes and save that into user_cpv
table. This is some of the code in my index view:
<table class="table table-striped table-bordered my-favourites">
<?php $form = ActiveForm::begin(['action' => ['user-cpv/create']]); ?>
<?php foreach ($values as $data): ?>
<tr>
<td><?= $data['cpvId'] ?></td>
<td><?= $form->field($model, 'cpv_id')->checkbox() ?></td>
</tr>
<?php endforeach ?>
<?php ActiveForm::end(); ?>
</table>
Here, I am stuck. When I am displaying CPV's ( lets take just their ids like in picture ), how to mark as checked those that are stored in user_cpv
relation table ? Here is some rough dirty pseudo code that I have in my mind :
actionIndex() {
// pull data from cpv table
$model = CPV::find();
// pull data from user_cpv
$relation = UserCpv::find();
// somehow find those that match and maybe pass that info to view ?
}
Then in my view, how to set cpv_ids found in user_cpv
as checked, if this is the checkbox render method ? :
<?= $form->field($model, 'cpv_id')->checkbox() ?>
And at last, if user check some other boxes, need to validate that he has chosen maximum of 3 boxes, and finally save that.
Can someone help me with this? Any help is appreciated, guidelines, example codes... Maybe my idea with user_cpv
relation table is wrong ? Thank you
For this first you need to define a relation in cpv model relationship with cpv-user model. In cpv model you will define relation (sample for relation model).
public function getUsercpv(){ return $this->hasMany(UserCpv::className(), ['id' => 'cpv_id']); }
now in controller you pull data from CPV model with relation
// pull data from cpv table $model = CPV::find()->joinWith('usercpv');
In view you can get user-cpv data using $data->usercpv, and can loop throug
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.