We’ll begin with creating the contacts model to have a nice database background, then we’ll build the widgets too. Accomplishing these tasks will require you to perform basically the same steps as before. For this reason, we’ll cover the steps to be taken very briefly, and will explain only new tasks in detail. If you get stuck with the model or the widgets, you can always look up what to do in previous chapters, namely:
- Creating the Accounts Model
- Creating the Accounts List
- Creating the Accounts Form
Creating the Contacts Model
The Contacts model will store the contact details (name, address, and how to reach info) of people you are in dialogue with and will be associated to an account (whether it’s a paying customer, or just a lead). The association is done with a relation between the contact model and the account model. We will set this after creating the model.
Establishing a Relation
Select the Account model, this will load its fields into the right pane. Choose the id field and press OK to close the wizard. Finally, click to the onDelete column, and select cascade.
Don’t forget to Save your changes!
By preforming the steps above, you’ve created a relation between two models. The account_id field of the Contact model is now related to the id field of the Account model. Such a relation is also called a foreign key.
So what’s the point of this?
Thanks to this relation, now each contact person can be linked to a client and naturally, a client may have any number of contacts linked to it. This saves you the trouble of typing in the company name over and over again when adding contacts, that is, reducing redundant information in the database. It also means less errors (no typos). Moreover, when a client gets deleted, all the contacts associated with it will be cleared out automatically as well (the cascade onDelete), again, saving you time and trouble.
But there is much more to foreign keys: they greatly increase efficiency too and allow programs like AppFlower to perform various useful operations quickly and easily that would be hardly possible otherwise. Another example of improvement is when you want to correct an account name, you don’t have to find all contacts with the old name and update them (which could be a heavy operation). Instead, this can be done in just one operation by changing the name in the account model and all relations will reflect the change.
Using relations, your application can do more and can do it more efficiently, providing faster and better results. The relation we just created is the most simple one. We’ll see examples of more advanced relations in the upcoming chapters.
Creating the List & Edit Widget
3. Creating List Widget Create a new module and name it contacts. Then launch Add Widget wizard and select contacts as module location, name it listContacts and select widget type list and click Next. Now we just need to add the fields we want to display in our list. Select contact model and choose id, account_id, first_name, last_name, title and email and save.
Creating the Edit Widget
4. Creating Edit Widget Once again click Add Widget, module location is contacts, name it editContact and widget type is Edit. Then proceed to Next step in the Wizard. Select the contact model and choose all fields to be included (except the id field). And hit Save.
That was a bit faster this time 🙂