Nested CFC/Object?

Hi,

I have a Customer.cfc and I'd like it to have a property that is
another object inside it. This other object is an Address.cfc

My hope is that when I <cfdump> out my Customer.cfc, it will have an
Address.cfc inside of it also.

How can I do this in ColdBox fashion?

Thanks,

West

Pass the Address Object in as a Property

Which version of ColdFusion are you using? Which version of ColdBox are you using? (doubt the later matters but I think it should start being in most questions just in case it helps.)

I'm using CF 9. So in the init method pass it in?

You can add these to ColdBox model…

/testObjects/Test.cfm

oCustomer = new testObjects.Customer();
writeDump(oCustomer);

/testObjects/Customer.cfc

/**

  • Customer
  • @accessors true
    */

component Customer displayname=“Customer” hint=“This is the Customer Object” output=“false”
{
property name=“ID” type=“numeric” displayname=“Unique ID” hint=“This is the Customers Unique ID”;
property name=“FirstName” type=“string” displayname=“FirstName” hint=“This is the Customers First Name”;
property name=“LastName” type=“string” displayname=“LastName” hint=“This is the Customers LastName”;
property name=“emailAddress” type=“string” displayname=“emailAddress” hint=“This is the Customers emailAddress”;
property name=“address” type=“any” displayname=“address” hint=“This is the Customers address”;

public function init()
displayname=“init” description=“init” output=“false”
{
var oAddress = new testObjects.Address();
setAddress(oAddress);
return this;
}

}

/testObjects/Address.cfc

/**

  • Address
  • @accessors true
    */

component Address displayname=“Address” hint=“This is the Address Object” output=“false”
{
property name=“ID” type=“numeric” displayname=“Unique ID” hint=“This is the Address Unique ID”;
property name=“Address1” type=“string” displayname=“Address1” hint=“This is the Address1”;
property name=“Address2” type=“string” displayname=“Address2” hint=“This is the Address2”;
property name=“City” type=“string” displayname=“City” hint=“This is the City”;
property name=“State” type=“string” displayname=“State” hint=“This is the State”;
property name=“Province” type=“string” displayname=“Province” hint=“This is the Province”;
property name=“Country” type=“string” displayname=“Country” hint=“This is the Country”;

public function init()
displayname=“init” description=“init” output=“false”
{
return this;
}

}

Not sure if this is the best way but this is what came to my mind.

test.cfm

oCustomer = new testObjects.Customer();
writeDump(oCustomer);

Customer.cfc

/**

  • Customer
  • @accessors true
    */

component Customer displayname=“Customer” hint=“This is the Customer Object” output=“false”
{
property name=“ID” type=“numeric” displayname=“Unique ID” hint=“This is the Customers Unique ID”;
property name=“FirstName” type=“string” displayname=“FirstName” hint=“This is the Customers First Name”;
property name=“LastName” type=“string” displayname=“LastName” hint=“This is the Customers LastName”;
property name=“emailAddress” type=“string” displayname=“emailAddress” hint=“This is the Customers emailAddress”;
property name=“address” type=“any” displayname=“address” hint=“This is the Customers address”;

public function init()
displayname=“init” description=“init” output=“false”
{
var oAddress = new testObjects.Address();
setAddress(oAddress);
return this;
}

}

Address.cfc

/**

  • Address
  • @accessors true
    */

component Address displayname=“Address” hint=“This is the Address Object” output=“false”
{
property name=“ID” type=“numeric” displayname=“Unique ID” hint=“This is the Address Unique ID”;
property name=“Address1” type=“string” displayname=“Address1” hint=“This is the Address1”;
property name=“Address2” type=“string” displayname=“Address2” hint=“This is the Address2”;
property name=“City” type=“string” displayname=“City” hint=“This is the City”;
property name=“State” type=“string” displayname=“State” hint=“This is the State”;
property name=“Province” type=“string” displayname=“Province” hint=“This is the Province”;
property name=“Country” type=“string” displayname=“Country” hint=“This is the Country”;

public function init()
displayname=“init” description=“init” output=“false”
{
return this;
}

}

This is the way I would do it. If you cannot get it working I can see if I can come up with a better example inside of ColdBox. This still should work.

Or passing it in like this

test.cfm

oAddress = new testObjects.Address(); oCustomer = new testObjects.Customer(oAddress); writeDump(oCustomer);

Customer.cfc

/**

  • Customer
  • @accessors true
    */

component Customer displayname=“Customer” hint=“This is the Customer Object” output=“false”
{
property name=“ID” type=“numeric” displayname=“Unique ID” hint=“This is the Customers Unique ID”;
property name=“FirstName” type=“string” displayname=“FirstName” hint=“This is the Customers First Name”;
property name=“LastName” type=“string” displayname=“LastName” hint=“This is the Customers LastName”;
property name=“emailAddress” type=“string” displayname=“emailAddress” hint=“This is the Customers emailAddress”;
property name=“address” type=“any” displayname=“address” hint=“This is the Customers address”;

public function init(pAddressObject)
displayname=“init” description=“init” output=“false”
{
setAddress(pAddressObject);
return this;
}

}

Address.cfc

/**

  • Address
  • @accessors true
    */

component Address displayname=“Address” hint=“This is the Address Object” output=“false”
{
property name=“ID” type=“numeric” displayname=“Unique ID” hint=“This is the Address Unique ID”;
property name=“Address1” type=“string” displayname=“Address1” hint=“This is the Address1”;
property name=“Address2” type=“string” displayname=“Address2” hint=“This is the Address2”;
property name=“City” type=“string” displayname=“City” hint=“This is the City”;
property name=“State” type=“string” displayname=“State” hint=“This is the State”;
property name=“Province” type=“string” displayname=“Province” hint=“This is the Province”;
property name=“Country” type=“string” displayname=“Country” hint=“This is the Country”;

public function init()
displayname=“init” description=“init” output=“false”
{
return this;
}

}

This makes sense, and thank you for the example. I'm going to try
that when I get in the office shortly.

Thanks!

So this way the address object is inside the customer object and is empty. When you create a new customer there are many ways to do it but here is one.

oAddress = new testObjects.Address(); newCustomer = new testObjects.Customer(oAddress); newCustomer.setFirstName("Jim"); newCustomer.setLastName("Sample"); newCustomer.setEmailAddress("Jim.Sample@gmail.com");

newCustomer.getAddress().setAddress1(“1 Abc Street”);
newCustomer.getAddress().setAddress2(“P.O. Box 1A”);
newCustomer.getAddress().setCity(“MyCity”);
newCustomer.getAddress().setState(“MyState”);
newCustomer.getAddress().setProvince(“MyProvinceCounty”);
newCustomer.getAddress().setCountry(“MyCountry”);

writeDump(newCustomer);

Create functions inside of Customer save(), insert(), update()

in Save check for ID of Customer and if no ID then insert if ID then Update.