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?



82.2k

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;
    }
}


3.6k

Thanks phalcon!!

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



3.6k

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?