Example Mura ORM Entities
To have a more cohesive understanding of how to work with Mura ORM, let's describe a simple application, and the objects we'll need to create for it.
Let's assume our client has asked us to create an "Address Book" type of application. This application would require an authorized user to be able to add, edit/modify, and/or delete Address Book entries. Each authenticated user would have their own Address Book. In other words, if Suzie logged in, she would only be able to see, create and/or modify her own entries.
Again, we're going to try and refrain from overcomplicating things. However, as a developer, It's easy to begin anticipating all kinds of enhancements and/or modifications you may want to make, and that's great. For now though, let's just stick to the script.
Address Book
Since a typical address book is filled with people, we'll start off by creating a Person object. Next, we'll have to consider the kind of properties or attributes a Person object should have. For example, they could have a first name, last name, zero or more phone numbers (PhoneNumber objects), zero or more addresses (Address objects), and so on.
The address book entry (Person) would be treated as a single object. It could be referenced by a single variable containing a pointer to the object. In addition, various helper methods could be associated with the object, such as a method to return the preferred phone number, the home address, and so on.
The following illustration is a sample UML diagram of our objects.
So, using our very basic objects, we can now begin to create our Mura ORM entities.
Person
Using our previous example, we could start our Person.cfc
with the following code.
component extends="mura.bean.beanORM" table="custom_persons" entityname="person" bundleable="true" displayname="PersonBean" public=true orderby="namelast,namefirst" { // primary key property name="personid" fieldtype="id"; // person attributes property name="namefirst" datatype="varchar" length="255" nullable=true; property name="namelast" datatype="varchar" length="255" nullable=true; // relationships property name="phonenumbers" singularname="phonenumber" cfc="personphonenumber" fieldtype="one-to-many" loadkey="personid" cascade="delete" orderby="phonetype"; property name="addresses" singularname="address" cfc="personaddress" fieldtype="one-to-many" loadkey="personid" cascade="delete" orderby="addresstype"; // Custom Methods public string function getFullName() { return get('namefirst') & ' ' & get('namelast'); } public any function getHomeAddress() { var rs = QueryExecute( (' SELECT addressid FROM custom_personaddresses WHERE addresstype = "Home" AND personid = :pid ') , { pid=get('personid') } ); return rs.recordcount ? getBean('personaddress').loadBy(addressid=rs.addressid) : getBean('personaddress').set('adddresstype', 'Home'); } }
PersonPhoneNumber
The PersonPhoneNumber.cfc
defines our PersonPhoneNumber entity.
component extends="mura.bean.beanORM" table="custom_personphonenumbers" entityname="personphonenumber" bundleable="true" displayname="PersonPhoneNumberBean" public=true { // primary key property name="phonenumberid" fieldtype="id"; // foreign key property name="person" fieldtype="many-to-one" cfc="person" fkcolumn="personid" nullable=true; // attributes property name="phonetype" datatype="varchar" length="255" nullable=true; // Home, Office, Mobile, etc. property name="phonenumber" datatype="varchar" length="255" nullable=true; }
PersonAddress
The following code comprises our PersonAddress.cfc
and will define our PersonAddress entity.
component extends="mura.bean.beanORM" table="custom_personaddresses" entityname="personaddress" bundleable="true" displayname="PersonAddressBean" public=true { // primary key property name="addressid" fieldtype="id"; // foreign key property name="person" fieldtype="many-to-one" cfc="person" fkcolumn="personid" nullable=true; // attributes property name="addresstype" datatype="varchar" length="255" nullable=true; // Home, Office, etc. property name="street1" datatype="varchar" length="255" nullable=true; property name="street2" datatype="varchar" length="255" nullable=true; property name="city" datatype="varchar" length="255" nullable=true; property name="state" datatype="varchar" length="255" nullable=true; property name="zip" datatype="varchar" length="255" nullable=true; }
Example
The example below illustrates how to use the entities we've created to get an iterator of a person's phone numbers.
contactBean = m.getBean('person').loadBy(persionid=m.event('pid')); // Phone Numbers Iterator itPhones = contactBean.getPhoneNumbersIterator(); while ( itPhones.hasNext() ) { phone = itPhones.next(); WriteOutput( phone.get('phonenumber') ); )