Question

I have a simple task to achieve and I don't know what's the best way of going about it.

I have a Listings table in my DB and I need to associate a list of specification which is a checkbox list the agent has to select.

The table looks something like this. - id - size - type - location

Now how do I go about storing the checkbox values? Do I create a new table "Listings_Specs" and associate each row with "Listings" table via "listing_id" or should I keep it in "Listings" table and add additional fields to it.

I am using Laravel 4 by the way.

Was it helpful?

Solution 3

This totally sounds like the kind of thing they might want you to add more of in the future. I'd shy away from creating columns for each specification you plan on tracking.

Instead, it would be a good idea to create a new model called Specification with a many-many relationship to Listings.

Checked state would then be dependent on a connection existing between Specification and Listing via the many to many join table.

OTHER TIPS

If listing and listing_specs have a 1 to 1 relationship, then no need for the table listing_specs, you can add fields to Listing table.

EDIT

if you have multiple values in a single field therefore you have to promote this field to a table.
`
Listing:
remove the field for the specs

Listing_specs:
ID | CONTENT | LIST_ID
1  | "fsafs" | 1
2  | "fsafs" | 1
3  | "fsafs" | 1
4  | "fsafs" | 2
5  | "fsafs" | 3`

It's hard to tell your the best way to go without knowing the specifics of your needs. In general, it is better to create a new table and associate it with the "Listings" table via "listing_id" like you mentioned. This gives you much more flexibility. Remember to index the "listing_id".

Anther way to do it is by including all the checkboxes directly in the "Listings" table. You can add a column for each checkbox. This is the easiest, but least flexible way. If the number of your checkboxes changes over time, you have to keep modifying the table.

Alternatively, you can add one single field for all checkboxes. Each byte of this field represents a checkbox, so the number of bytes should be the same as the number of your checkboxes and with the same order (or you have to specify the order somewhere). This is a little more complex and is also not very flexible because you have to modify this field every time you change the checkboxes. However, the advantage is that you use one field which can be handy if you have many checkboxes. If you prefer more readability, you can use characters (i.e. 1 and 0) instead of bytes.

Example: Let's say you have 5 checkboxes, the "Listings" table will look like:

ID | specs
1  | "10110"
2  | "01100"
3  | "11100"
4  | "00010"
5  | "01010"

So, I'd go with the first solution that you mentioned, but it really depends your needs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top