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

Affected rows?

Hi!!

My question is: In a custom model It's possible to know if a select has affected to some row?

Something like that:


class MyModel extends \Phalcon\Mvc\Model
{
    public function customSQL()
    {
        $mysql = 'DELETE FROM XXX WHERE condition=1';

        $this->getModelsManager()->executeQuery($mysql);
        return ($this->getReadConnection()->affectedRows()!=null) && 
                $this->getReadConnection()->affectedRows()>0;
    }
}

Even on sql success, always $this->getReadConnection()->affectedRows() is null ... :(

Any idea?, what am I doing wrong?



98.9k

Currently the query you're executing is processed first by PHQL and then it's executed one by one, so probably that code won't give you the expected result, you could better try:

class MyModel extends \Phalcon\Mvc\Model
{
    public function customSQL()
    {
        $mysql = 'DELETE FROM XXX WHERE condition=1';
        $this->getWriteConnection()->execute($mysql);
        return ($this->getReadConnection()->affectedRows()!=null) && 
                $this->getReadConnection()->affectedRows()>0;
    }
}


6.0k

Thanks phalcon!!

But.. not works :( again always null and the select is working well.



6.0k

Oh! I'm fool!!!

You are right! , I have an active bd transaction and not exists rows affected... If i deactivate transactions return one row.

So... this means , in bd transactions I cannot check If the db operation has worked fine?

I don't think that it's because of your transaction. I also use the Transaction but it works and return 1.