Seconded on the updateCollection feature, that would be seriously helpful. Otherwise (for those who still need it), I would solve it like so:
$stack = $modx->getCollection('modelName', array(
'key' => 'value'
));
foreach($stack as $object) {
$object->set('key', 'value2');
$object->save();
}
// or
foreach($stack as $object) {
$object->fromArray(array(
'key' => 'value2',
'editedon' => time()
));
$object->save();
}
// Edit: code-wise I don’t like it, but I
think it’s possible.
$stack = $modx->getCollection('modelName', array(
'key' => 'value'
));
foreach($stack as $object) {
$object->set('key', 'value2')->save();
}
Hope this helps!
@Jason; hehe. :-P
@MarkGHErnst
Developer at Adwise Internetmarketing, the Netherlands.
-
MODX Staff
- 730 Posts
They are, because getCollection returns an array of xPDO objects.
You do have to ->save() each of them however.
Alternately, if you have a relationship where there is a parent for all of these objects, you could load them with ->getMany(), iterate the array, update each object, then just call ->save() on the parent object.
Oh crap indeed netProphET! I forgot all about ->save(); See, $stack litteraly returns a STACK of OBJECTS in ARRAY format. Looping over them would push a single object (row in MySQL) into the variable which you can manipulate to your wishes (within reason, of course).
I updated my example with the ->save();
@MarkGHErnst
Developer at Adwise Internetmarketing, the Netherlands.
Alright! I also added a small quick save function but I am not sure if it works.
->set(’key’, ’value’)->save();
This only works if the set() function returns the specified object.
@MarkGHErnst
Developer at Adwise Internetmarketing, the Netherlands.
-
MODX Staff
- 730 Posts
https://github.com/modxcms/xpdo/blob/master/xpdo/om/xpdoobject.class.php
->set returns boolean Determines whether the value was set successfully and was determined to be dirty (i.e. different from the previous value).
Oh yes of course, that’s pretty logical since I often use it in the if ($object->save()) { method.
@MarkGHErnst
Developer at Adwise Internetmarketing, the Netherlands.