Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>Update with new answer based on comment thread in OP</h1> <p>Since you're extending the base Vote object form VoteLike.cfc, the CFCs share a local thread-safe variables scope (so long as you're not storing this in Application or Server scope where it can be fiddled with by others). This means that you set a value, such as Variables.Post, once, and reference that in any function within the stack of CFCs.</p> <p>So change your getPost() function to this:</p> <pre><code>beforeValidation("getPost"); private function getPost(){ Variables.Post = model("post").findByKey(this.postID); } </code></pre> <p>Now, within any function in either VoteLike.cfc, you can reference Variables.Post. This means that you have a single instance of Post in the local CFCs variables scope, and you don't have to pass it around via arguments or other scopes.</p> <h1>Original Answer below</h1> <p>The "most correct" way to handle this would be to pass the object to each individual function as an argument. That, or add the Post as a property of the Vote object, so that the Vote object has access to the full Post object.</p> <pre><code>&lt;cffunction name="doTheValidation"&gt; &lt;cfargument name="ThePost" type="Post" required="true" /&gt; &lt;cfargument name="TheVote" type="Vote" required="true" /&gt; &lt;!--- do stuff here ---&gt; &lt;/cffunction&gt; </code></pre> <p>The reason this is a bad practice is because you're requiring that your objects have access to an external scope, that may or may not exist, in order to do their work. If you decided that you wanted to deal with multiple votes or posts on a single page, you then have to fiddle with the external scope to get things to work right.</p> <p>You're far better off passing your object around, or using composition to piece your objects together in such a fashion that they're aware of each other.</p> <h1>Update</h1> <p>Regarding performance issues, if you use composition to tie your objects together, you end up with just two objects in memory, so you don't have to instantiate a bunch of unnecessary Post objects. Also, passing a CFC into a function as an argument will pass the CFC by reference, which means that it's not creating a duplicate of the CFC in memory, which should also take care of your performance concerns.</p> <h1>Code Sample Update</h1> <p>To illustrate the "by reference" comment above, set up the following files and put them in a single directory on their own.</p> <p><strong>TestObject.cfc</strong>:</p> <pre><code>&lt;cfcomponent output="false"&gt; &lt;cfproperty name="FirstName" displayname="First Name" type="string" /&gt; &lt;cfproperty name="LastName" displayname="Last Name" type="string" /&gt; &lt;/cfcomponent&gt; </code></pre> <p><strong>index.cfm</strong>:</p> <pre><code>&lt;!--- Get an instance of the Object ---&gt; &lt;cfset MyObject = CreateObject("component", "TestObject") /&gt; &lt;!--- Set the initial object properties ---&gt; &lt;cfset MyObject.FirstName = "Dan" /&gt; &lt;cfset MyObject.LastName = "Short" /&gt; &lt;!--- Dump out the object properties before we run the function ---&gt; &lt;cfdump var="#MyObject#" label="Object before passing to function" /&gt; &lt;!--- Run a function, sending the object in as an argument, and change the object properties ---&gt; &lt;cfset ChangeName(MyObject) /&gt; &lt;!--- Dump out the object properites again, after we ran the function ---&gt; &lt;cfdump var="#MyObject#" label="Object after passing to function" /&gt; &lt;!--- Take a TestObject, and set the first name to Daniel ---&gt; &lt;cffunction name="ChangeName"&gt; &lt;cfargument name="TheObject" type="TestObject" required="true" hint="" /&gt; &lt;cfset Arguments.TheObject.FirstName = "Daniel" /&gt; &lt;/cffunction&gt; </code></pre> <p>You'll notice when you run index.cfm, that the first dump has the FirstName as Dan, while the second has the FirstName as Daniel. That's because CFCs are passed to functions by reference, which means that any changes made within that function are made to the original object in memory. Hence no recreation of objects, so no performance hit.</p> <p>Dan</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload