Jason
Jason

Reputation: 1977

Does CF ORM have an Active Record type Update()?

Currently I am working partly with cfwheels and its Active Record ORM (which is great), and partly raw cfml with its Hibernate ORM (which is also great).

Both work well for applicable situations, but the thing I do miss most when using CF ORM is the model.update() method that is available in cfwheels, where you can just pass a form struct to the method, and it will map up the struct elements with the model properties and update the records.. really good for updating and maintaining large tables. In CF ORM, it seems the only way to to update a record is to set each column individually, then do a save. Is this the case?

Does cf9 ORM have an Active Record type update() (or equivalent) method which can just receive a struct with values to update and update the object without having to specify each one?

For example, instead of current:

member = entityLoadByPK('member',arguments.id);
member.setName(arguments.name);
member.setEmail(arguments.email);

is there a way to do something like this in CF ORM?

member = entityLoadByPK('member',arguments.id);
member.update(arguments);

Many thanks in advance

Upvotes: 2

Views: 198

Answers (2)

Sergey Galashyn
Sergey Galashyn

Reputation: 6956

In my apps I usually create two helper functions for models which handle the task:

/*
* Get properties as key-value structure
* @limit Limit output to listed properties
*/
public struct function getMemento(string limit = "") {

    local.props = {};

    for (local.key in variables) {
        if (isSimpleValue(variables[local.key]) AND (arguments.limit EQ "" OR ListFind(arguments.limit, local.key))) {
            local.props[local.key] = variables[local.key];
        }
    }

    return local.props;

}


/*
* Populate the model with given properties collection
* @props Properties collection
*/
public void function setMemento(required struct props) {

    for (local.key in arguments.props) {
        variables[local.key] = arguments.props[local.key];
    }

}

For better security of setMemento it is possible to check existence of local.key in variables scope, but this will skip nullable properties.

So you can make myObject.setMemento(dataAsStruct); and then save it.

Upvotes: 1

barnyr
barnyr

Reputation: 5678

There's not a method exactly like the one you want, but EntityNew() does take an optional struct as a second argument, which will set the object's properties, although depending on how your code currently works, it may be clunky to use this method and I don;t know whether it'll have any bearing on whether a create/update is executed when you flush the ORM session.

If your ORM entities inherit form a master CFC, then you could add a method there. Alternatively, you could write one as a function and mix it into your objects.

I'm sure you're aware, but that update() feature can be a source of security problems (known as the mass assignment problem) if used with unsanitized user input (such as the raw FORM scope).

Upvotes: 1

Related Questions