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

How to perform update on model with expression syntax?

Hi

When I try to update a row (I'll use Robots as in dosc as example), this is not working:

$robot = Robots::find(2);

//do some stuff

$robot->update(array('views' => 'views + 1'));

How do I perform such updates where value is no a strict value but an expression?

Would this work?

$robot->update(array('views'=>$robot->views + 1))?

I realize it's not exactly what you were asking, but it would accomplish the same thing.

Maybe cleaner way ;) Better readable than less dirty lines of code ;)

$robot = Robots::find(2);
$robot->views++;
$robot->update();


2.2k

Thanks guys.

I guess Phalcon models can't do expressions (yet, why not?), but when I thought about it... the only scenario with model would be the only one that can be done like you wrote in your examples - based on existing data that can be used in query, omitting expressions. Other ones (like mass update of n rows) should be done using, and works only with, PHQL.

So I guess that is it regarding this subject. Cheers!

It is ORM, if you need to use SQL you can use PHQL.

I don't know what you want to achieve, but if you need to update $robot->views on every update, you should do it in your model

class Robots extends \Phalcon\Mvc\Model
{
        public function beforeUpdate()
        {
                 $this->view++;
        }
} 


2.2k

No, no. I was just curious if I could do expression calls using model, if yes then how, that is just it. No particular reason.