Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Cannot delete a row - primary key is not set, but it is

item->delete() do not allow me to delete a row from DB.

  • DB Permissions are correct.
  • Phalcon version (3.0.3)

DDL:

CREATE TABLE `z_cb_colors` (
  `color_id` int(11) NOT NULL AUTO_INCREMENT,
  `description` varchar(128) DEFAULT NULL,
  PRIMARY KEY (`color_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

i do a simple test:

$item = ZCbColors::findFirst(7);  // this return model with color_id 7
$item->getColorId() // this return number 7
$item->delete();  // this throws ->Cannot delete the record because the primary key attribute: 'color_id' wasn't set

idk why is this happening...

EDIT:

  • if i debug, $item has color_id = 7

  • if i call ->jsonserialize() color_id is null

AUTO_INCREMENT=8 , so does the 7th item even exists ?

edited Jan '17

yea, sure there are 10,i never specify that Autoincrement, let me change something, thanks for pointing that, ill test

edited Jan '17

i recreate the table, SQL files do not specify any auto increment amount, that text is extracted from Idea DB DDL, and shows again the AI 8, seems like a bug from idea

when y call from terminal show this:

show create table zcbcolors

CREATE TABLE z_cb_colors (

color_id int(11) NOT NULL AUTO_INCREMENT,

description varchar(128) DEFAULT NULL,

PRIMARY KEY (color_id)

) ENGINE=InnoDB DEFAULT CHARSET=utf8

and i did the test again, with same results.

And what metadata adapter you are using?

so why are you using $item->getColorId() , try to use $item->colorId , maby that could be the issue

edited Jan '17

@HudsonNicoletti the problem is not what value i get from module, i just cant delete, and... im using --get-set when i generate the models, and field value that i get is ok.

@Jurigag the Metadata adapter im using is:

use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter

$di["modelsMetadata"] = function () {

$metadata = new MetaDataAdapter();

return $metadata;

};

database is working in previous calls, select, insert, but this is the first delete and fails

you can see here the generated model:

<?php

namespace LyCore\Modules\Backend\Models;

/** * ZCbColors * * @package LyCore\Modules\Backend\Models */ class ZCbColors extends \Phalcon\Mvc\Model {

/**
 *
 * @var integer
 * @Primary
 * @Identity
 * @Column(type="integer", length=11, nullable=false)
 */
protected $colorId;

/**
 *
 * @var string
 * @Column(type="string", length=128, nullable=true)
 */
protected $description;

/**
 * Method to set the value of field colorId
 *
 * @param integer $colorId
 * @return $this
 */
public function setColorId($colorId)
{
  $this->colorId = $colorId;

  return $this;
}

/**
 * Method to set the value of field description
 *
 * @param string $description
 * @return $this
 */
public function setDescription($description)
{
  $this->description = $description;

  return $this;
}

/**
 * Returns the value of field colorId
 *
 * @return integer
 */
public function getColorId()
{
  return $this->colorId;
}

/**
 * Returns the value of field description
 *
 * @return string
 */
public function getDescription()
{
  return $this->description;
}

/**
 * Initialize method for model.
 */
public function initialize()
{
  $this->hasMany('colorId', 'LyCore\Modules\Backend\Models\ZCbBikiniColors', 'colorId', ['alias' => 'ZCbBikiniColors']);
}

/**
 * Returns table name mapped in the model.
 *
 * @return string
 */
public function getSource()
{
  return 'z_cb_colors';
}

/**
 * Allows to query a set of records that match the specified conditions
 *
 * @param mixed $parameters
 * @return ZCbColors[]
 */
public static function find($parameters = null)
{
  return parent::find($parameters);
}

/**
 * Allows to query the first record that match the specified conditions
 *
 * @param mixed $parameters
 * @return ZCbColors
 */
public static function findFirst($parameters = null)
{
  return parent::findFirst($parameters);
}

}



107.2k
Accepted
answer

And how your columnMap method looks like? MetaDataAdapter what exactly is this class?

i updated the previous post because is missing the alias of metadata adapter, (Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter)

another clue is ->save() and update() makes a new row

i dont have columnMap, but im not sure the annotations isn't doing that job?

with column map, works, but i thought that phalcon translates underscores to camel case, and it works in other tables without columnMap, why?

And how your columnMap method looks like? MetaDataAdapter what exactly is this class?

edited Jan '17

phalcontools --mapcolumn do the job, that creates the columnMap(), and maps underscored fields with camel.

and now my question is... why ---camelize and --mapcolumn are not together, as i understand when i camelize i MUST map.

@Jurigag ty for all you feedback, was very useful

No, phalcon don't translate underscores to camel case. It works because there is no underscore in table.

 $columns = $this->getModelsMetaData()->getAttributes($this);
        $map = [];
        foreach ($columns as $column) {
            $map[$column] = lcfirst(Text::camelize($column));
        }
        return $map;

There you have code to camelize. Just add this to some AbstractModel and extend every model with it.