ColdBox persisting form variables to database with ORM
Tags: Coldfusion, Coldbox
I had a user ping me and ask for some examples on how to do this, so I thought I would give a very quick blog post on how easy it is with ColdBox.
Coupled with a previous post that I did on validation, it makes it rather easy all round.
So how easy is it to persist form variables to a DB with ORM and ColdBox? Answer is very, the only catch to making your life easy is to name the forms to the same as the entity. So if you have a form similar to this
<input type="test" name="password" />
<input type="test" name="name" />
<input type="test" name="phone" />
We can then use a handler to then process this information, it will be as simple as writing a handler like this.
var rc = event.getCollection();
var formTest = ORMService.get('entity');
BeanFactory.populateBean(target=formTest);
}
Now this is very basic stuff, and not much to it at all. So if you have named your form variables to that of the entity, then by just getting the object first and then using the populateBean, we have now populated the entity. The last thing left to add then is the saving of the entity, so by adding one more line we now have this.
var rc = event.getCollection();
var formTest = ORMService.get('entity');
BeanFactory.populateBean(target=formTest);
ORMService.save(entity=formTest);
}
And that's all there is to it, just make sure you inject the ORMService and BeanFactory into the handler.
There are no comments for this entry.



TweetBacks