how can i persist Ip address automatically.

27 views
Skip to first unread message

salman khan

unread,
Apr 8, 2013, 10:49:13 AM4/8/13
to cf-or...@googlegroups.com
hi,
   I would like to know how can i persist ip address automatically in the database when any transaction took place.

thanks



Matt Quackenbush

unread,
Apr 8, 2013, 10:51:33 AM4/8/13
to cf-or...@googlegroups.com
By writing code that handles that for you?  That's a pretty vague question. What have you tried?  What worked?  What didn't work?  What error message did you receive?  Etc.






--
You received this message because you are subscribed to the Google Groups "cf-orm-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cf-orm-dev+...@googlegroups.com.
To post to this group, send email to cf-or...@googlegroups.com.
Visit this group at http://groups.google.com/group/cf-orm-dev?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Seth Johnson

unread,
Apr 8, 2013, 10:53:36 AM4/8/13
to cf-or...@googlegroups.com

You can use event handlers for that.  Any time an object is saved it can store this for you.

Seth

Brian Kotek

unread,
Apr 8, 2013, 11:04:33 AM4/8/13
to cf-or...@googlegroups.com

salman khan

unread,
Apr 8, 2013, 11:37:10 AM4/8/13
to cf-or...@googlegroups.com
I am using this code... when user submits comments it should persist the Ip address of that user.
 property name="Id" column="Forumboard_commentId" type="numeric" fieldtype="id" unique="true" generated="insert" generator="identity";
             property name="Body" column="Forumboard_body" type="string" ormtype="text";
             
             
             
             //relate comment with post.
             property name="Discussion" fieldtype="many-to-one" fkcolumn="comment_Forumboard_DiscussionId" cfc="Discussion" lazy="true";
             //relate comment with User.
             property name="User" fieldtype="many-to-one" fkcolumn="comment_Forumboard_userId" cfc="User"  MissingRowIgnored="true" lazy="true";
             
             
             //our created and updated functions.
              property name="Created" column="comment_created" type="date" ormtype="timestamp" setter="false" notnull="true";
              property name="Updated" column="comment_updated" type="date" ormtype="timestamp" setter="false" notnull="true";
              property name="Ipaddress" column="Forumboard_ipaddress" type="string" setter="false";
 
            public function preInsert() returntype="void" output="false" hint="I am called by the ORM just before a record is inserted" {
                // Our Created date is only ever set the once
                  Variables.Created = now();
                  preUpdate();
            }

           public function preUpdate() returntype="void" output="false" hint="I am called by the ORM just before a record is updated" {
               // Our Updated date changes every time the post changes
              Variables.Updated = now();
           }
          
           public function getIp() returntype="any" output="false" {
               return CGI.REMOTE_ADDR;

Matt Quackenbush

unread,
Apr 8, 2013, 11:42:20 AM4/8/13
to cf-or...@googlegroups.com
You have preInsert() and preUpdate() to set the Created and Updated properties, but you're not calling getIp() in either one. Add that call and you should be good to go.

salman khan

unread,
Apr 8, 2013, 12:08:01 PM4/8/13
to cf-or...@googlegroups.com
I have tried to call getIp() in both the preInsert() and preUpdate() method... I get error 127.0.0.1 is not type of data or time.
but when i try like by removing setter="false" and setting Ipaddress while saving Comment like Comment.setIpaddress(CGI.REMOTE_ADDR); it works fine. I want it to be happen automatically.

Thanks

Matt Quackenbush

unread,
Apr 8, 2013, 12:14:17 PM4/8/13
to cf-or...@googlegroups.com
So `variables.Ipaddress = getIp()` throws that exception?  That doesn't seem possible. How about the full code and stack trace?

Brian Kotek

unread,
Apr 8, 2013, 12:22:19 PM4/8/13
to cf-or...@googlegroups.com
On Mon, Apr 8, 2013 at 12:08 PM, salman khan <salmank...@gmail.com> wrote:
I have tried to call getIp() in both the preInsert() and preUpdate() method... I get error 127.0.0.1 is not type of data or time.
but when i try like by removing setter="false" and setting Ipaddress while saving Comment like Comment.setIpaddress(CGI.REMOTE_ADDR); it works fine. I want it to be happen automatically.

For future reference, stating this in your initial question would have saved everyone a lot of time. If you have a problem, post this sort of information. Don't make other people dig it out of you. Thanks. 

Brian Kotek

unread,
Apr 8, 2013, 12:26:18 PM4/8/13
to cf-or...@googlegroups.com
To be clear, you get this error when you do:

public function preInsert() returntype="void" output="false" hint="I am called by the ORM just before a record is inserted" {
Variables.Created = now();
Variables.Ipaddress = getIp();
preUpdate();
}

?

salman khan

unread,
Apr 8, 2013, 1:58:38 PM4/8/13
to cf-or...@googlegroups.com
yeah Exactly... I am really sorry for not to be clear about my issue.
Thanks!

Brian Kotek

unread,
Apr 8, 2013, 2:06:11 PM4/8/13
to cf-or...@googlegroups.com
That doesn't make sense. The Ipaddress property you show above is of type string. If you're getting an error about it not being a date, then either you've got a some other type conversion issue, or what you're actually doing is different than what you showed above.

Also, you probably don't want those two date columns to be of type timestamp. A date/time is plenty.

Seth Johnson

unread,
Apr 8, 2013, 3:47:10 PM4/8/13
to cf-or...@googlegroups.com

Adobe ColdFusion or Railo?  Sorry if you already mentioned.

--

salman khan

unread,
Apr 9, 2013, 11:36:45 AM4/9/13
to cf-or...@googlegroups.com
before I was making mistake by not calling it in globaleventhandler.
if(structKeyExists(arguments.entity,"setIpAdress")){
   arguments.entity.setIpAddress(CGI.REMOTE_ADDR);
  }
I have put this code in PreUpdate() method inside globaleventHandler and now it works exactly.

Thanks all for help.
Reply all
Reply to author
Forward
0 new messages