phalcon / phalcon-devtools

Phalcon Developer Tools
https://docs.phalcon.io/latest/en/devtools
Other
1.33k stars 631 forks source link

[BUG] Mismatch of capitalization between model properties and column names in generated model files #1463

Closed Nyde closed 3 years ago

Nyde commented 4 years ago

Please see the discussion on the forum (https://forum.phalcon.io/discussion/20661/modelsave-saves-not-the-entire-model-return-xy-is-required) and the accepted answer (https://forum.phalcon.io/discussion/20661/modelsave-saves-not-the-entire-model-return-xy-is-required#C63301).

Actual Behavior

When you create a model from a table that has capitalized columns, the property is saved in lower case. However, the column map exspects the field name with a leading capital letter, therefore yields "xy is required" on save().

See this table definition:

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;

Note the capitalized "B" of "Barcode".

The model generated by webtools.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' // leading B is capitalized, but the corresponding field is all lower case
        ];
    }

}

When trying to save(), this yields "Barcode is required", even if set. Internally, $this->barcode gets set by setBarcode('somestring');, but the column map generated by webtools.php exspects the field to be named $this->Barcode.

Exspected behaviour

webtools.php should match the keys from the column map with the actual property names, with correct capitalization taken into account.

Details

BeMySlaveDarlin commented 3 years ago

This is NFR. Wont be done, because of camelize behavior, which is needed for fields named like group_id. Changing template behavior to make Barcode be consistent breaks snake_cased fields name mutation. As said in forums thread, ill recommend to rename fields such as Barcode to barcode or manually change columnMap() method to fulfill that needs.

BeMySlaveDarlin commented 3 years ago

Somehow fixed in #1498