Cphalcon: Object of class Phalcon\Db\RawValue could not be converted to float

Created on 4 Apr 2017  路  17Comments  路  Source: phalcon/cphalcon

Expected and Actual Behavior

Describe what you are trying to achieve and what goes wrong.

Provide output if related. Provide coredump if any. Use https://github.com/phalcon/cphalcon/wiki/Generating-a-backtrace as reference

<?php
namespace Tasks;

use PhalconExt\Cli\Task;

class LogsTasks extends \Phalcon\Mvc\Model
{

    /**
     *
     * @var integer
     * @Primary
     * @Identity
     * @Column(type="integer", length=10, nullable=false)
     */
    public $id;

    /**
     *
     * @var double
     * @Column(type="double", length=9, nullable=true)
     */
    public $exec_time;

    /**
     *
     * @var integer
     * @Column(type="integer", length=10, nullable=false)
     */
    public $pid;

}

class IssueTask extends Task
{

    /**
     * @help("袙芯褋锌褉芯懈蟹胁械写械薪懈械 芯褕懈斜泻懈")
     * @paramsFormat("<y>cli.php {task} {action}</y>")
     * @shortcut("12766")
     * @param type $param
     */
    public function issue12766Action($parameters = null)
    {
        $model = new LogsTasks;
        $model->pid = 1212;
        $model->save();

        $model->exec_time = 0.1212;

        $saved = $model->update();
    }
}

DB Scheme

CREATE TABLE `logs_tasks` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,  
  `exec_time` decimal(9,3) DEFAULT NULL,
  `pid` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `pid` (`pid`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

When update model get error

Notice: Object of class Phalcon\Db\RawValue could not be converted to float in Z:\OpenServer\server\trunk\app\cli\tasks\IssueTask.php on line 56

Details

  • Phalcon version: 3.1.1
  • PHP Version: PHP 7.0.15 (cli) (built: Jan 17 2017 13:44:27) ( NTS )
  • Operating System: Win 7
  • Database: MariaDB-5.5-x64

Most helpful comment

Created PR.

All 17 comments

@Jurigag Could you please take a look

Yea, can you provide your table schema?

@Jurigag added in start post

What happens if you do $model->exec_time = floatval(0.1212);

@409H same error on $this->update() line

Can't reproduce.

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$di = new FactoryDefault();
$di->set(
    'db',
    function () {
        $adapter = new \Phalcon\Db\Adapter\Pdo\Mysql(
            [
                'host'     => 'localhost',
                'username' => 'root',
                'password' => '',
                'dbname'   => 'phalcon_test',
                'options'  => [
                    PDO::ATTR_EMULATE_PREPARES   => false,
                    PDO::ATTR_STRINGIFY_FETCHES  => false,
                    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
                ],
                'charset'  => 'utf8',
            ]
        );

        return $adapter;
    }
);
$di->set('modelsMetadata', function (){
   $metadata = new Model\MetaData\Apc();
   $metadata->setStrategy(new Model\MetaData\Strategy\Annotations());

   return $metadata;
});

require "LogsTasks.php";

$model = new LogsTasks;
$model->pid = 1212;
$model->save();

$model->exec_time = 0.1212;

$saved = $model->update();

var_dump($saved); // true

Checked on php 7.1.1 but i doubt that on 7.0 there will be difference

Screenshot
What am I doing wrong?

I test this code on Phalcon 3.0.4 - work fine.
On 3.1.0 same error

I don't know, you sure you posted full code of your model? Just with this code you posted i don't have error.

@Jurigag i ran you code and get error

Fatal error: Uncaught Error: Class 'FactoryDefault' not found

What version you use?

Oh i forgot use statements:

use Phalcon\Di\FactoryDefault;
use Phalcon\Mvc\Model;

Your example work fine on Phalcon 3.1.1. Thanks!
I'll figure out what I've done

Maybe you need to clear metadata? I don't know really.

Try this example please.

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

use Phalcon\Di\FactoryDefault;
use Phalcon\Mvc\Model;

class ModelDynamicUpdate extends \Phalcon\Mvc\User\Plugin
{

    /**
     * @var boolean
     */
    protected $dynamicUpdate;

    /**
     * @param boolean $dynamicUpdate
     */
    public function __construct($dynamicUpdate = true)
    {
        $this->dynamicUpdate = $dynamicUpdate;
    }

    /**
     * @param \Phalcon\Events\Event               $event
     * @param \Phalcon\Mvc\Model\ManagerInterface $modelsManager
     * @param \Phalcon\Mvc\ModelInterface         $model
     */
    public function afterInitialize(\Phalcon\Events\Event $event, \Phalcon\Mvc\Model\ManagerInterface $modelsManager, \Phalcon\Mvc\ModelInterface $model)
    {
        $modelsManager->useDynamicUpdate($model, $this->dynamicUpdate);
    }
}

$di = new FactoryDefault();
$di->set(
    'db', function () {
    $adapter = new \Phalcon\Db\Adapter\Pdo\Mysql(
        [
        'host' => 'localhost',
        'username' => 'root',
        'password' => '',
        'dbname' => 'phalcon_test',
        'options' => [
            PDO::ATTR_EMULATE_PREPARES => false,
            PDO::ATTR_STRINGIFY_FETCHES => false,
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
        ],
        'charset' => 'utf8',
        ]
    );

    return $adapter;
}
);

$di->set('modelsManager', function () {

    $eventsManager = new \Phalcon\Events\Manager;
    $modelsManager = new Model\Manager;
    $eventsManager->attach("modelsManager", new ModelDynamicUpdate(true));
    $modelsManager->setEventsManager($eventsManager);

    return $modelsManager;
});



require "LogsTasks.php";

$model = new LogsTasks;
$model->pid = 1212;
$model->save();

$model->exec_time = 0.1212;

$saved = $model->update();

var_dump($saved); // true

?>

Yea, now i see, let me fix it.

Created PR.

Fixed in the 3.1.x branch.

Was this page helpful?
0 / 5 - 0 ratings