We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Update resultset

Looking at the documentation, a resultset should have an update() function I should be able to use like:

public function initialize(){
    $this->hasMany('session_id','Store\Model\ItemHold','session_id',['alias'=>'ItemHold']);
}
...
public function updateHold(){
    $this->ItemHold->update(['date'=>date(DATE_MYSQL)]);
}

However, I get an error saying the function isn't found. Looking at the documentation for the class, there is no mention of the update() function.

So, does it exist? Is there any way to update every record in a resultset?



12.1k

As you relate the ItemHold as a hasMany relationship you get a resultsetInterface by calling $this->ItemHold. So to update every record you have to loop and update.

foreach ($this->ItemHold as $itemHold) {
    $itemHold->update(['date'=>date(DATE_MYSQL)]);
}

I didn't try this ;)

If there is a solution for your last qustion let us know :)

The documentation clearly shows though, that the update() function is available on the ResultSet: https://docs.phalcon.io/en/latest/reference/models.html#updating-related-records