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

SkipOperation

Hi, I have this code in my model:

protected function beforeCreate() {
    $subs = $this->findFirst("user_id = $this->user_id AND to_id = $this->to_id AND type = $this->type");
    if ($subs)
        $this->skipOperation(true);
}

And it really checks to see if such a record already and call skipOperation(); Nevertheless, the operation is still running.

What am I doing wrong?



58.4k
edited Nov '14

I think you're wrong, method findFirst is static so you can not call this, try this

    Model::findFirst()

No, it's working properly. I test all in xdebug and if row exist, then call skipOperation, but insert still occurs.

@phalcon, can you help me?



98.9k

skipOperation is only intended to work with the 'delete' operation right now, rather than that you can return false to stop the current operation.



98.9k

Also, your code is prone to SQL injections, consider use bound paramters.

edited Dec '14

Ok, about injections, but if I return false, then i need something like this:

if (!$notySubs->save() && array_key_exists(0, $notySubs->getMessages()))
    throw new ErrorException($notySubs->getMessages());

It's no good :( Can you extend skipOperation for all CRUD operations in next release?



98.9k

skipOperation does not make what you think it does, it just changes the current operation from 'delete' to 'update'

But it is not logical, well, or at least I would like to see it differently :)

Okay, I do not insist, but such a thing still could be useful not only for me.