Yii framework: populate data from related table - php

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

Related

Dynamically add custom input fields and save them to database

I want to develop an web-service where a user can add tickets. Every ticket has got an input text field title and a textarea description. If the user klick the save button, the data will be saved in a mysql database.
The admin has got an admin panel. He can add or remove input fields in this admin panel to change the add ticket view form the user.
For example: The admin adds a select field category. You can select category A, B or C. If you select category A, there will be a new input text field called animal. If you select category B, nothing happens. If you select category C, there will be 2 new fields: A text field and a number field. The number field is required. And so on, and so on. After a week, the admin could remove some fields, or add a category or... To conclude, the admin can add and remove select, text, number, password,... fields to the add ticket section with relationships, length and requirements.
I dont know how to structure the database and to save the data. I think about something like a mysql table tickets with title, desc, and data and put an XML / JSON String to the data field and another table ticketFields with name (category, animal,...), type (text, number,...), required (yes/no), length (int), data (to store data for select fields). The problem is, that the relationships are missing in this model. So how can I save this data efficient?
The relational model might look like this:
Ticket:
id PK
Answer:
ticketId FK PK
fieldId FK PK
value
Field:
id PK,
name,
parentFieldId NULL FK,
parentFieldValue NULL,
type,
required,
min NULL,
max NULL,
range NULL
(...other "constraint" fields, checked regarding choosen type)
At first, fields with parentFieldId of NULL are displayed. Fields having parentFieldId set are shown only if the answer for parent field is given. Fields having parentFieldId and parentFieldValue set are displayed if the answer for parent field is given and it equals parentFieldValue.
Given answer is checked regarding to field's type (e.g. if type is "number" then the answer must be a number between min and max).
You can create another table with id, category and field for the admin to use and relate them both using a common id field, preferably primary key. Now, this table can be queried for the reference of new categories inserted against a particular id in your main table. Use javascript/jquery to dynamically create html code for the new fields and show them on your page.
Suppose your the user selects Option 'A', then the new table can be queried to see if there are any fields set by the admin against the option 'A'. If yes, retrive those fields and show them to the user.

Form DataBase Design

I'll try to explain my problem and let´s see if I get some help.
I`m trying to design a DB. This data base will contain users and forms but my problem is how to design it to save forms.
The forms will be created by the administrator when he needs them, so in the DB I have a table to save the form name. Another table with all the possible fields for all the forms and another table where form-names and fields will be save so I can see all the field that a determinated form has.
An example: In FormName table I have 3 form names: Personal Info, Computer Knowledgement and hobbies. And in Fields table I have, for example, Name, Address, Phone, Operating Systems, Programming, Bike ridding, playing pc games.
I decide to create a form with name number 1 (Personal info) and fields number 1,2,3 (Name, Address, Phone). This information is recorded in another table called full-form where it´s saved form name id and field id. This is pretty clear for me (perhaps I´wrong).
The tricky part comes now. Some user decide to fill the personal info form so the name of the form and each field will has to displayed. When the user fill the form it has to be saved some way so the administrator can see the name of the form, each field that that form has, who filled the form and the information written in each field. And all this should be displayed not only in the DB but in some different way like a table, for example.
By the way... I´m using cakephp and mysql.
Thanks in advance,
Abraham
As far as I understand, setup looks like this. As you said, we have 3 tables:
'formname' = id, name (for form names)
'formfields' = id, fieldname, attributes (for form names, added attributes in case you need them)
'forms' = id, formname_id, formfields_id, formfields_order (ready forms and possibility to order field in form)
And also you should create additional table where to store submitted values
'forms_values' = id, forms_id, value (in this case one row for each field)
If you want to relate those values to specific user, one more table should be created
'users_forms' = forms_values_id, user_id
Of course there should be users table as well.
My suggestion is to drop table 'formnames' and put form name in 'forms' table.
Then table 'forms' will contain
id, form_name, formfields_id, formfields_order
Hope this answer will put you forward :)

Fill list from different table based on primary id

I have 3 table which have relation with each others... In 1st table, i have student details contains studentid (PK), studentname, dob, etc...
Table 2 contain course consist of coursecodeid(PK), studentid(FK), coursename, teachername and etc..
Table 3 contain grade consist of studentid(fk), courseid(fk), id(pk_autoincrement) and grade.
When system starts, studentid will be load into a dropmenu. Onchange this dropmenu, second dropmenu will be shown and load with courseid take by selected student (based on studentid)
When user select courseid onchange a label will be visible to show student grade.
Everything done in 1 page... Anybody can give me sample code either in php or javascript since i am using php and mysql database.
Ajax. That is the way to go. Check here for ideas, this is doing something similar to what you are trying to achieve. Check this for more abstract code reference. This is a possible duplicate. And generally, searching for loading combobox based on other combobox jquery will give you pretty decent results.

Yii - Foreign key - Access Data

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.

Should I Populate Country/Region Names at DB or Application Level?

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.

Categories