3 Replies Latest reply on Oct 25, 2006 2:42 PM by Newsgroup_User

    CFC Question

    fshin
      Hi,

      I'm pretty new to CFCs and am re-working an application I made a while back. Basically, I have a User cfc and also a UserGateway cfc. When a user logs in, a User object is instantiated which stores their data in the session scope. However, if the user is an admin, they can also create or update users. In the update user form, another User object is instantiated with the selected user's properties. The question is, does this persist into the action page? Also, what if I do this multiple times. Does each instance need to be named something different? How long does the instatnce persist?

      Thanks,
      Frank
        • 1. Re: CFC Question
          Level 7
          The question is, does this persist into the action page?

          If you put the instanciated object into a variable scope that persists
          such as session, application and server. Otherwise no it dies with all
          other non-persistent variables of a request.


          Also, what if I do this multiple times. Does each instance need to be
          named something different?

          Or put into different indexes of an array or structure so that a new
          instant of the object does not overwrite a previous instant of the
          object. YES.


          How long does the instance persist?

          Exactly as long as you define it to persist. Put it into short lived
          scopes such as variables and request it lives only as long as the
          current request. Put it into longer lived scopes such as session,
          application and server, it lives as long as these scopes live.


          Remember an instant of an object is just a very complex variable, but a
          variable none the less and follows all the general rules of variables
          simple or otherwise.
          • 2. Re: CFC Question
            fshin Level 1
            So if this is true, how do I update a user if I first have to instantiate the User object to load the "current" properties into the form? On the action page, do I have to create a new object and update it? Or if I put it into session, after the update, do I kill that object somehow? confused...
            • 3. Re: CFC Question
              Level 7
              Normally You build a way in the component(aka object) that allows you to
              update the data of the component. You can do this by making the
              data(aka state or members) public and thus directly writable. Or, more
              often considered the best practice, you would create methods(aka
              functions) that accept parameters to update the data of the object.

              One way this could be done.

              In the CFC
              ----------
              <cffunction name="setStatus" ...>
              <cfargument name="statusValue" ...>
              <!--- Do what needs to be done with the status value, if anything --->

              <cfset variables.status = arguments.status>
              </cffunction>

              In the action
              -------------
              <cfset session.myUserObjVar.setStatus(form.newStatus)>