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

Model save doesn't work with metaData

I tried to use manual metaData for model. But when I call save method I got error Notice: Undefined index: 9 in ... Phalcon\Mvc\Model\Exception: The meta-data is invalid or is corrupt in ...

When I comment metaData in model all works good

Here is code example:

    public function metaData()
    {
        return array(

            //Every column in the mapped table
            MetaData::MODELS_ATTRIBUTES => array(
                'heroId', 'name', 'level', 'raceId', 'classId', 'userId', 'locationId', 'xp',
                'maxHp', 'currentHp', 'maxMp', 'currentMp', 'attack', 'armor'
            ),

            //Every column part of the primary key
            MetaData::MODELS_PRIMARY_KEY => array(
                'heroId'
            ),

            //Every column that isn't part of the primary key
            MetaData::MODELS_NON_PRIMARY_KEY => array(
                'name', 'level', 'raceId', 'classId', 'userId', 'locationId', 'xp',
                'maxHp', 'currentHp', 'maxMp', 'currentMp', 'attack', 'armor'
            ),

            //The identity column, use boolean false if the model doesn't have
            //an identity column
            MetaData::MODELS_IDENTITY_COLUMN => 'heroId',

            MetaData::MODELS_DATA_TYPES_NUMERIC => array(
                'locationId' => true,
            )
        );
    }

    public function columnMap()
    {
        return array(
            'heroId' => 'heroId',
            'name' => 'name', 
            'level' => 'level', 
            'raceId' => 'raceId', 
            'classId' => 'classId', 
            'userId' => 'userId', 
            'locationId' => 'locationId',
            'xp' => 'xp',
            'maxHp' => 'maxHp',
            'currentHp' => 'currentHp',
            'maxMp' => 'maxMp',
            'currentMp' => 'currentMp',
            'attack' => 'attack',
            'armor' => 'armor',
        );
    }

And table schema:

CREATE TABLE `heroes` (
  `heroId` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(45) NOT NULL,
  `level` int(11) NOT NULL DEFAULT '1',
  `raceId` int(11) DEFAULT NULL,
  `classId` int(11) DEFAULT NULL,
  `userId` int(11) DEFAULT NULL,
  `locationId` int(11) DEFAULT NULL,
  `xp` int(10) unsigned NOT NULL DEFAULT '0',
  `maxHp` int(11) unsigned NOT NULL,
  `currentHp` int(11) unsigned NOT NULL,
  `maxMp` int(11) unsigned NOT NULL,
  `currentMp` int(11) unsigned NOT NULL,
  `attack` int(10) unsigned NOT NULL,
  `armor` int(10) unsigned NOT NULL,
  PRIMARY KEY (`heroId`),
  UNIQUE KEY `name_UNIQUE` (`name`),
) ENGINE=InnoDB AUTO_INCREMENT=26498 DEFAULT CHARSET=utf8

Most likely what has happened is you haven't defined your meta-data properly. There is not much documentation on what the meta-data should look like, and I've noticed if it's not perfect, Phalcon throws that error.

I've found the best way to handle meta-data is to not define it at all when you're developing/building. When that's done, use APC to store the meta-data. This way it's generated by Phalcon itself, so you know it's good.

If you want help with your manual declaration, please post your meta-data code, as well as the schema of the database table the model maps to.



1.7k

Added code example.

Look here: https://docs.phalcon.io/en/latest/reference/models.html#manual-meta-data, you're missing a lot of stuff you need to define.