ColdMVC

Getters and Setters

You can access your model's properties several ways.

Option 1: You can use ColdFusion's built-in getters and setters for persistent properties.

var book = _Book.new();

book.setTitle("The Da Vinci Code"); // setter

var title = book.getTitle(); // getter

Option 2: You can use your model's prop method.

var book = _Book.new();

book.prop("title", "The Da Vinci Code"); // setter

var title = book.prop("title"); // getter

Option 3: You can access each property as a method via onMissingMethod().

var book = _Book.new();

book.title("The Da Vinci Code"); // setter

var title = book.title(); // getter

It can be easier to use either option 2 or 3 when working with relationships as ColdMVC will automatically cast empty values to NULL and handle building relationships for you.

For example, if each Book had a relationship to an Author, here is how you would update a Book with an ID of 2 to have an Author with an ID of 5 for each of the options listed above.

Option 1:

var book = _Book.get(2);

var author = _Author.get(5);

book.setAuthor(author);

Option 2:

var book = _Book.get(2);

book.prop("author", 5);

Option 3:

var book = _Book.get(2);

book.author(5);

As you can see, options 2 and 3 are considerably shorter than option 1.