23.02.2015 Views

www.it-ebooks.info

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Models and saving data<br />

Before jumping straight into creating our models, we need to define clearly what<br />

type of models we are going to build and how many. So let's review our example<br />

scenario. For our gift registry, <strong>it</strong> appears that we will need two different models:<br />

Chapter 4<br />

• Registry Model: This model is used to store the gift registry <strong>info</strong>rmation,<br />

such as gift registry type, address, and recipient <strong>info</strong>rmation<br />

• Registry Item: This model is used to store the <strong>info</strong>rmation of each of the gift<br />

registry <strong>it</strong>ems (quant<strong>it</strong>y requested, quant<strong>it</strong>y bought, product_id)<br />

Although this approach is correct, <strong>it</strong> does not meet all the requirements of our<br />

example scenario. By having all the registry <strong>info</strong>rmation stored into a single table, we<br />

cannot add more registry types w<strong>it</strong>hout modifying the code.<br />

So, in this case, we will want to break down our data into multiple tables:<br />

• Registry Ent<strong>it</strong>y: This table is used to store the gift registry and event<br />

<strong>info</strong>rmation<br />

• Registry Type: By storing the gift registry type into a separate table, we can<br />

add or remove event types<br />

• Registry Item: This table is used to store the <strong>info</strong>rmation of each of the gift<br />

registry <strong>it</strong>ems (quant<strong>it</strong>y requested, quant<strong>it</strong>y bought, product_id)<br />

Now that we have defined our data structure, we can start building the<br />

corresponding models that will allow us to access and manipulate our data.<br />

Creating the models<br />

Let's start by creating the Gift Registry type model, which is used to manage the registry<br />

types (wedding, birthday, baby shower, and so on). To do so, follow these steps:<br />

1. Navigate to the Model folder on our module directory.<br />

2. Create a new file called Type.php and copy the following content into the file<br />

(the file location is app/code/local/Mdg/Giftregistry/Model/Type.php):<br />

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!