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

Problem when saving model data: Cannot use a scalar value as an array

Hi,

When saving model data I'm getting this notice: Cannot use a scalar value as an array in phalcon/mvc/model.zep on line 1194.

The problem happens when I specify PDO::ATTR_DEFAULT_FETCH_MODE=>PDO::FETCH_OBJ when creating the PDO object. BTW, the record gets saved properly, but it generates the notice. When I remove that option, the notice is gone.

Does anyone know how I can get it fixed and still fetch my results as objects?

Here's my setup:

Phalcon 3.4.0, Linux Mint 18.3, Mysql 5.7.23

Here's a simple test case:

$di = new \Phalcon\DI\FactoryDefault();

$di->set('db', function() {
    return new \Phalcon\Db\Adapter\Pdo\Mysql([
        "host" => "localhost",
        "username" => "...",
        "password" => "...",
        "dbname" => "...",
        "options" => [
            PDO::ATTR_DEFAULT_FETCH_MODE=>PDO::FETCH_OBJ,
        ],
    ]);
});

class AffiliateSettings extends \Phalcon\Mvc\Model
{
}

$settings = new AffiliateSettings();
$settings->user_id = rand(1, 9999);
$settings->save(); // <-- notice generated here

The MySQL table:

CREATE TABLE affiliate_settings (
user_id INT UNSIGNED NOT NULL,
enabled TINYINT(1) NOT NULL DEFAULT 1,
PRIMARY KEY (user_id)
) ENGINE = InnoDB DEFAULT CHARSET utf8 COLLATE utf8_unicode_ci;
edited Aug '18

Well, it might be a bug, there is check like num["rowcount"] in phalcon code but in your case num is object. Why you exactly need FETCH::OBJ? If you use ORM you have objects returned so not sure why you need this option.

When using models, yes, I get objects, but when I run $this->db->query() to execute direct queries it returns arrays unless PDO::FETCH_OBJ is specified.