Cphalcon: [BUG]: $modelInstance->save(); doesn't recognize values, treats them as null

Created on 31 May 2020  路  4Comments  路  Source: phalcon/cphalcon

Describe the bug
An instance of a model gets filled with data. var_dump($modelInstance) shows all fields populated. When using $modelInstance->save(), some fields become NULL values, therefore throwing "fieldname is required". After the save(), var_dump shows the values as desired. If I set a default value inside the table, the "missing" value gets this default.

Also, print_r() with $modelInstance->toArray() doesn't output the values, while var_dump does: https://forum.phalcon.io/discussion/20661/modelsave-saves-not-the-entire-model-return-xy-is-required#C63287

A full explaination on this can be found at https://forum.phalcon.io/discussion/20661/modelsave-saves-not-the-entire-model-return-xy-is-required

Also, I experienced a similiar issue some weeks ago: https://forum.phalcon.io/discussion/20567/query-tells-field-is-required-but-is-set

Besides of the issue described in the forum post, I got to experience the issue with another, much smaller table.

To Reproduce
Steps to reproduce the behavior:

CREATE TABLE `barcodes` (
  `cid` int(11) NOT NULL AUTO_INCREMENT,
  `aid` int(11) NOT NULL,
  `Barcode` varchar(20) COLLATE utf8_bin NOT NULL,
  PRIMARY KEY (`cid`),
  KEY `articleFK_idx` (`aid`),
  CONSTRAINT `articleFK` FOREIGN KEY (`aid`) REFERENCES `articles` (`aid`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

The definition of articles can be found in the forum post, but it shouldn't be relevant to this issue.

The corresponding model (generated by webtools.php, removed comments):

<?php

class barcodes extends \Phalcon\Mvc\Model
{
    protected $cid;
    protected $aid;
    protected $barcode;

    public function setCid($cid)
    {
        $this->cid = $cid;

        return $this;
    }

    public function setAid($aid)
    {
        $this->aid = $aid;

        return $this;
    }

    public function setBarcode($barcode)
    {
        $this->barcode = $barcode;

        return $this;
    }

    public function getCid()
    {
        return $this->cid;
    }

    public function getAid()
    {
        return $this->aid;
    }

    public function getBarcode()
    {
        return $this->barcode;
    }

    public function initialize()
    {
        $this->setSource("barcodes");
        $this->belongsTo('aid', '\articles', 'aid', ['alias' => 'articles']);
    }

    public static function find($parameters = null): \Phalcon\Mvc\Model\ResultsetInterface
    {
        return parent::find($parameters);
    }

    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    public function columnMap()
    {
        return [
            'cid' => 'cid',
            'aid' => 'aid',
            'Barcode' => 'Barcode'
        ];
    }

}

Code for testing:

$barcodeArray = array('123', '456');

foreach($barcodeArray as $code)
{
    $bc = new barcodes();

    $bc->setAid($aid);
    $bc->setBarcode($code);

    var_dump($bc->barcode);
    echo $bc->getBarcode()."<br />";

    if ($bc->save() == false) 
    {
        foreach ($bc->getMessages() as $message)
            echo $message."<br />";
    }

    echo $bc->getBarcode();
}

Output:

D:\path\to\htdocs\SomeController.php:208:string '123' (length=3)
123
Barcode is required
123

Expected behavior
The model should keep the values and store them in the database.

Details

  • Phalcon version: 4.0.6
  • PHP Version: 7.4.4
  • Operating System: Windows 10
  • Installation type: downloading the precompiled Windows DLL
  • Zephir version (if any): Version 0.12.17-6724dbf
  • Server: Apache
  • Other related info (Database, table schema): see above

Additional context
Works without any problems at other models.

bug not a bug

Most helpful comment

Could you please post your model as well?

I edited the issue text with the model.

All 4 comments

Could you please post your model as well?

Could you please post your model as well?

I edited the issue text with the model.

I'll close this issue, because the problem got solved in the forum thread; problem was the mismatch between the capitalization I made in the database and the one webtools chose for the models properties. Therefore, the column map tried to access values that were not set, yielding a (correct) 'xy is required'.

Thank you for reporting back.

Was this page helpful?
0 / 5 - 0 ratings