I'm going through a tough decision (at least for me)...
My website would have a list of countries in dropdown list and list of cities based on chosen country. I decided to populate the list at db level as:
CREATE TABLE Country
(
countryID - PK
countryName - FK
);
CREATE TABLE City
(
cityID - PK
countryID - FK
cityName
);
CREATE TABLE Register
(
registerID - PK
cityID - FK
)
However, this can add some complexity in php back-end coding when inserting record into registration as well as retrieving record from countries/cities, because on registration form user will city name on droplist, user won't see cityID, so i will have to fetch cityID based on chosen cityName, etc. Therefore, i said why do i just put the countries and cities list at application level in fixed drop down list and make register table look as:
CREATE TABLE Register
(
registerID - PK
cityName
)
cityName gets inserted directly without us having to join or call multiple tables and get specific ID for a city so we can then grab the cityName, etc...
I will only normalize in crucial tables such as making relationships between Member and Post/Thread tables. A member can have multiple threads one-to-many relationship. Otherwise, things that relate to multi-value as countries list won't be considered at DB level for simplicity and rap sake.
What do you think? Advice ...
The nature of the data should drive the database model. Not your front end issues.
Why not load your cities into a drop down list? Set the display text=cityName. Set the value=cityID.
Not sure I understand the text of your question completely, but to answer the title of your question, I would keep country names in the database. Countries/Cities do change their names from time-to-time.
I would suggest defining XML with the country name/id as the element and city names as the child elements. It makes it edit/read the xml data. I'm not sure whether you would like to consider this.
Related
I am uncertain of the best way to store multiple select options into mysql. Initially, I created a comma separated list of selected values. I have read that this is bad programming practice and also had a difficult time trying to figure out how to search the three columns in my db with the comma separated varchar value (eg: 1,5,17). It seems the best practice would be to create a 3rd table that would insert 3 records for the example above. This table would be linked via an id.
Example:
Table 1 (age levels)has fields id & age level (baby,teenager,adult)
Table 2 has classes id & class name
Table 3 is to store the multiple select options of WHO can attend each class. I guess the fields would be id & classID & ageLevelID
My questions are as follows:
1. how do I efficiently insert this data into table 2 and table 3?
2. how do I pull it all together to search for a person who is looking for a class that allows for just babies and teenagers, for example?
Relating 2 tables is easy, relating 3 tables is a conceptual struggle. Also, am I correct that it is asking for trouble to store comma delimited values in one field? Thank you.
Table agelevel (ageid (unique), agelevel)
Table classes (classid (unique), classname)
Table classesinfo (classid, fieldname,fieldvalue)
With that you could have any 'fieldname' such as agelevel, detailed description, hours, etc and then the value of that field in fieldvalue. This allows your classes to any an infinite number variables attached to a single classid.
I have two tables Customer and Location. Location has fields id, country, zone, state and city. Customer table has a foreign key location_id.
While creating a customer, I need to display dropdowns for country, zone, state and city on the create customer form and the values in the dropdown should be populated from the location table. When a user selects these fields, the corresponding location id should be saved in the location_id field of the Customer table.
While updating a customer, values for country, zone, state and city dropdowns should be populated in the form based on the location_id foreign key. Any changes in the values of these dropdowns should be saved as location_id foreign key.
Any inputs or pointers to any existing posts/articles will be of great help.
Any inputs or pointers to any existing posts/articles will be of great help.
OK, some hints
1.
values in the dropdown should be populated from the location table.
For this, i think, you need to get acquainted with the post on dependent dropdowns in yii for populating one dropdown when choosing from another one.
2.
When a user selects these fields, the corresponding location id should be saved in the location_id field of the Customer table.
For this you might need to do some ajax call upon dropdown/textField selection/changing. You might do this by external js function but i recommend the Select2 with Yii.
For example here on item change in Select2 the ajax call is performed to the controller eventsPerformance and action save with data as serialization of the whole form js:$("#performance-form").serialize():
$this->widget('ext.select2.ESelect2',array(
'attribute' => 'subStatus',
'model'=>$performance,
'data'=>$subStatusArray,
'htmlOptions'=>array(
'onChange'=>CHtml::ajax(array('type'=>'POST',
'url'=>'eventsPerformance/save',
'data'=>'js:$("#performance-form").serialize()',)),
),
));
Feel free to ask if some more help needed.
Here is nothing special. Just use methods whose name starts with active and your dropdowns will preselect right option. I believe you are using Gii to create your CRUD operations. So you need only to add Location dropdows in users update / create view. E.g.
$user = User::model()->findByPk(99);
$locations = Location::model()->findAll();
$locationList = CHtml::listData($locations, 'id', 'country');
echo CHtml::activeDropDownList($user, 'location_id', $locationList);
But I think your description is not correct because in your Location table one ID identify whole combination of country-zone-state-city, i.e.:
1 | USA | NULL | Arizona | Phoenix
2 | ... | ...
If you need drop-downs for every of them, you need to create separate DB tables.
// countries
id | title
// cities
id | country_id | title
Im trying to create a checklist system eg. a list of items to collect. the user will be able to add the list to their profile and then as they collect the items then can check the box for an item in the list click submit and the checked item will now be marked as collected. I have it coded and working fine but it makes an insane amount of queries to the database to work.
i have 5 tables. a users table (for the registered users username, id..etc), a lists table (containing the list name, description, id ), a list items table (containing the individual items. id, title and listID (to reference the list it belongs to)). userslist table (for the lists the user has added to their profile. userID and listID) and collecteditems table (this has the list items that a person has checked the box for as collected. listItemsID, UserID)
the problem is when i view the mylists.php page it will query the userslist table and return all the ID's for the lists the user has added. then once i have the ID for the list it then queries the list table to find out what the name of the list is (this could mean having to make 10 queries to the list table if i have 10 different lists added). if i added a listname column to the userslist table i would only need to make 1 query for the page and that is to the userslists table and i could construct the page with that 1 query.
First, I wouldn't worry about queries on primary keys. All your tables have a primary key referenced by other tables. These will use joins.
Second, you don't have to get the list names separately. Use a query such as:
select l.listName
from UserLists ul join
Lists l
on ul.listId = l.listId
where userId = $userid
This will return all the names in a single query.
It sounds like you should keep your data normalized (that is, avoid the redundant data) and instead gather all the data you want in a single query, by using a JOIN.
How are this two concepts work together ?
I have a scenario
city table
country table
city.country_id is a FK to country.id
Objective
fetch all the cities and display the country name also
My problem
the fetch method will get the cities from the table
if I need the country name I would have to do an extra search for it or an inner join
but by doing so I make extra queries when they are not necessary (display just the a city info for example)
Question
What is the right way to apply the Data Gateway Pattern in this case.
You should use a join if you need h associated country name. Simply add another method like fetchWithCountry.
Another option is to create a view of the city table that includes the CountryName. Then get your DataGateway to select using the view and save using the table. In your domain objects make the CountryName field a dumb read only field. This approach might seem like a dirty hack but it does simplfy things.
I' m just getting into Yii and am finding it a little confusing (at least compared to Cake or CI). Here's a couple things I'm wondering about:
1) If I have an 'add station' form (using a Station model). In the form, there will be a drop down list of 'Companies' (from Company Model). Station belongsTo Company (Station has a foreign key of company_id).
How do I go about generating the drop down list? Should the Php code used to pull the data be stored in the model, or should it just be a line in the form?
2) I also have the following database tables:
station
- id
- location_id
location
- id
- state_id
- city_id
state
- id
- state
city
- id
- city
For right now, in the same form as above, I wanted to have a 'Location' field, and then 2 drop downs for city and state. Whatever the user chooses, would then be stored in the location table as foreign key pairs. How would I go about doing that?
1) Look at my answer to this question for tips about the downdown (the key is CHtml::listData):
Does CDbcommand method queryAll() in yii return indexed entries only?
2) Is location just a relation table between state and city? Then just do a MANY_MANY relation() in your City and State models. The AdvancedAR extension makes life a little easier for managing MANY_MANY releationships.
Otherwise, just set up a Model for each of your tables, and add the appropriate relations() between them. Then in your form post action, create a new Location and set the state_id and city_id, and save() it.