Hi everyone,

 

Ok, seems to be a big weekend for new features.  Here’s one some of you might find useful: multi-step linking relationships. 

 

With older versions of reactor you could establish relationships between two separate objects via linking relationships.  IE: If you have three tables, user, userAddress, address.  A userAddress hasOne of both User and Address and User and Address have many of each other this is what your xml would look like:

 

<object name="User">

            <hasMany name="Address">

                        <link name="UserAddress" />

            </hasMany>

</object>

 

<object name="UserAddress">

            <hasOne name="User">

                        <relate from="userId" to="userId" />

            </hasOne>

            <hasOne name="Address">

                        <relate from="userId" to="userId" />

            </hasOne>

</object>

 

<object name="Address">

            <hasMany name="User">

                        <link name="UserAddress" />

            </hasMany>

</object>

 

This would allow you to get an Iterator of all Addresses for a user and an Iterator of all Users for an address.  However, let’s say that you have an additional table, State.  And let’s say you want to get an Iterator of all states your users are in.  Previously you’d have to create your own custom method.  Now, you could update your xml to look like this:

 

<object name="User">

            <hasMany name="Address">

                        <link name="UserAddress" />

            </hasMany>

            <hasMany name="State">

                        <link name="UserAddress" />

                        <link name="Address" />

            </hasMany>

</object>

 

<object name="UserAddress">

            <hasOne name="User">

                        <relate from="userId" to="userId" />

            </hasOne>

            <hasOne name="Address">

                        <relate from="userId" to="userId" />

            </hasOne>

</object>

 

<object name="Address">

            <hasMany name="User">

                        <link name="UserAddress" />

            </hasMany>

            <hasOne name="State">

                        <relate from="stateId" to="stateId" />

            </hasOne>

</object>

 

<object name="State" />

 

Now, from your UserRecord you could call getStateIterator() and you would end up with a query of all the states your users are in.  This will not automatically add a distinct so you’ll want to update your Iterator to only return distinct rows in this case.

 

This works by automatically creating a join from User to UserAddress to Address to State and only returning rows from State.

 

Also, linking relationships don’t care what type of relationship they link through.  You can link through hasOne or hasMany.  Furthermore, only one relationship needs to be defined between two objects.  From the previous example, the Address is configured to have one state, but a state isn’t configured to have any addresses.  This gives you a lot of power to establish relationships however you need.

 

So, give it a try again and let me know if you have any problems.

 

Doug

 

 

 

-- Reactor for ColdFusion Mailing List -- [email protected] -- Archives at http://www.mail-archive.com/reactor%40doughughes.net/

Reply via email to