Multiple Pk Choice From Checkboxlist

Hi there. What should I do if I want to choose multiple pk with checkboxlist and then use them to insert proper columns and still preserve them as primary keys.

For example I have checkboxlist with primary keys:

How I can do it if I want to insert Name+lastname+5 country_id`s ??

I know I can implode list and then insert it but then they are like simple string values. How I can insert them and still use like PK ? For example when I need to updateByPk.

And to be clear i don’t want to use any ext like multimodelform (there are to many issues with them). So is there any possible way of achieving that ??

Have you tried to insert pk’s list as string values separated by commas?

So in your table you will have:

name text;

lastname text;

selected_id text; (where selected_id is in the form ‘3,5,109,5435,4324’);

When you have to use updateByPk, you can explode your "selected_id" string so:




explode(',', $selected_id);



Hmm but is this rly a proper way of doing this ?? I just think it’s like the easiest trick or am I wrong with such idea ??

I think this is very simple to implement.

Hi mentorq

Never insert anything separated by commas (or anything else) in a database. That’s not what relational dbs are designed for. The proper solution would be to have three tables. User, Country and User_Country (or whaterever you want to name them). User holds firstname and lastname. Country holds the name (of the country). Then User_Country is used to store a relation between two entries of User and Country. Of course you can add as many relations as you need.

See the Yii guide for more about this. The three tables tbl_post, tbl_post_category and tbl_category in the very first figure on page Relational Active Record show exactly what you need.

mentorq was searching for a simple solution. Then if you need only referencing to other tables without need to mantain integral reference, separated commas could be a simple solution.

Ok I get your idea, but how I should implement this ? 3 tables, 1 form. With 1 submit should I loop it in the controller ?? (1 checkboxlist I want to pick all PKeys and insert proper values - for example for user John Doe add 5 countrys, its very similar to multimodelform http://www.yiiframework.com/extension/multimodelform/ but i don’t want to use it.)

Aswer for this will solve about 50% of my problems. I want to use this method e.g. for communication system (pick users from list and send them msg based on chosen PK) But still I’m looking for way to solve this. Tabluar input but not extension …

You can have as many models as you like in your form. It is actually pretty easy:

Step 1:

Get all your users and all your countries from the db in your controller. Display the users in a dropdown and your countries as checkboxes (or however you like) in your form.

Step 2:

In your controller check for $_POST[‘User’] and $_POST[‘Country’]. If both are set instantiate for each country a new UserCountry model. Set both the user id and the country id in that model and save it. That’s it.

Step 3:

You can now access for each user all related countries using a MANY_MANY relation in your model.

Hope that helps :) Btw: I highly recommend you to read through the Yii user guide. Especially the Active Record part.

Yea I understand what should I do but it’s still so complicated ;] I will do my best, thx for help.