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.

Can't Update Primary Key

Hi all,

Since some days, I have a problem to update two primary keys in table and I don't find why. That's weird because other fields are updated.

Example : ( langueId = 1; currencyId = 1)

$country = Country::findFirst(1);
$country->langueId = 2;
$country->currencyId = 1;
$country->isoCode = 'en' ;
$country->isoCode = '';
$country->updatedAt = date('Y-m-d H:i:s');
$country->update();

No Changes for langueId and currencyId. They still the same value after update; Can someone help plz ?

My Sample Country Model

<?php

namespace Some\Model;

class Country extends \Phalcon\Mvc\Model
{
    public $id;

    public $langueId;

    public $currencyId;

    public $isoCode;

    public $active;

    public $createdAt;

    public $updatedAt;

    public function initialize()
    {
        $this->belongsTo("langueId", "Some\Model\Langues", "id", array('alias' => 'langue'));
        $this->belongsTo("currencyId", "Some\Model\Currency", "id", array('alias' => 'currency'));
    }

    public function getSource()
    {
        return 'countries';
    }
}

My Sample Currency Model

<?php

namespace Some\Model;

class Langues extends \Phalcon\Mvc\Model
{

    public $id;

    public $isoCode;

    public $languageCode;

    public $active;

    public $createdAt;

    public $updatedAt;

    public function initialize()
    {
        $this->hasOne("id", "Some\Model\Country", "langueId", array( 'alias' => 'country'));
    }

    public function getSource()
    {
        return 'langues';
    }
}

My Sample Country Model

<?php

namespace Some\Model;

class Currency extends \Phalcon\Mvc\Model
{
    public $id;

    public $name;

    public $isoCode;

    public $isoCodeNum;

    public $symbol;

    public $active;

    public $createdAt;

    public $updatedAt;

    public function getSource()
    {
        return 'currencies';
    }

    public function initialize()
    {
        $this->hasOne("id", "Localization\Model\Country", "currencyId", array( 'alias' => 'country'));
    }
}

SQL Schema

-- -----------------------------------------------------
-- Table `langues`
-- -----------------------------------------------------
`langues` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `isoCode` VARCHAR(3) NULL,
  `languageCode` VARCHAR(5) NULL,
  `active` TINYINT(1) NULL,
  `createdAt` DATETIME NULL,
  `updatedAt` DATETIME NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `currencies`
-- -----------------------------------------------------
`currencies` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(32) NULL,
  `isoCode` VARCHAR(3) NULL,
  `isoCodeNum` VARCHAR(3) NULL,
  `symbol` VARCHAR(8) NULL,
  `active` TINYINT(1) NULL,
  `createdAt` DATETIME NULL,
  `updatedAt` DATETIME NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `countries`
-- -----------------------------------------------------
`countries` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `langueId` INT UNSIGNED NOT NULL,
  `currencyId` INT UNSIGNED NOT NULL,
  `isoCode` VARCHAR(3) NULL,
  `active` TINYINT(1) NULL,
  `createdAt` DATETIME NULL,
  `updatedAt` DATETIME NULL,
  PRIMARY KEY (`id`, `langueId`, `currencyId`),
  INDEX `fk_countries_langues1_idx` (`langueId` ASC),
  INDEX `fk_countries_currencies1_idx` (`currencyId` ASC),
  CONSTRAINT `fk_countries_langues1`
    FOREIGN KEY (`langueId`)
    REFERENCES `langues` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_countries_currencies1`
    FOREIGN KEY (`currencyId`)
    REFERENCES `currencies` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

Any Idea ?



1.2k
edited Sep '15

have a try

    $country = Country::findFirst(1);
    $country->langueId = 2;
    $country->currencyId = 1;
    $country->isoCode = 'en' ;
    $country->isoCode = '';
    $country->updatedAt = date('Y-m-d H:i:s');
    if($country->save() === false) 
    {
        foreach($coutry->getMessages() as $message)
        {
            echo $message->getMessage();
        }
    }

langueId and currencyId is a FOREIGN KEY. So you need to check

    $country->langueId = 2;
    $country->currencyId = 1;

are there in parent table?



3.4k

It's what I do

$form = new formCountry();
$country = Country::findFirst($id);
$form->bind($this->request->getPost(), $country, array('isoCode', 'langueId', 'currencyId', 'active'));                                                    

if ($form->isValid() === true) {                         
    if ($country->update() == false) {
        foreach ($country->getMessages() as $message) {
            $messages[] = array( 'type' => 'error', 'msg' => (string)$message);
        }
    } else {
        ...
    }
}

I check post value, keys and they are not empty. Foreign keys exists in parent tables. It's why is weird.



1.2k
edited Sep '15

$country->update() === false

It's what I do

>$form = new formCountry();
>$country = Country::findFirst($id);
>$form->bind($this->request->getPost(), $country, array('isoCode', 'langueId', 'currencyId', 'active'));                                                    
>
>if ($form->isValid() === true) {                         
>    if ($country->update() == false) {
>        foreach ($country->getMessages() as $message) {
>            $messages[] = array( 'type' => 'error', 'msg' => (string)$message);
>        }
>    } else {
>        ...
>    }
>}

>

I check post value, keys and they are not empty. Foreign keys exists in parent tables. It's why is weird.



3.4k

Tested but same, no update error :(

$country->update() === false

It's what I do

```php $form = new formCountry(); $country = Country::findFirst($id); $form->bind($this->request->getPost(), $country, array('isoCode', 'langueId', 'currencyId', 'active'));

if ($form->isValid() === true) {
if ($country->update() == false) { foreach ($country->getMessages() as $message) { $messages[] = array( 'type' => 'error', 'msg' => (string)$message); } } else { ... } } ```

I check post value, keys and they are not empty. Foreign keys exists in parent tables. It's why is weird.



1.2k
edited Sep '15

I'm sorry, I need to sleep now

I will try tomorrow

which version did you use?

Tested but same, no update error :(



3.4k

Thx for your try :) I use version 2.0.7

I'm sorry, I need to sleep now I will try tomorrow which version did you use?

Tested but same, no update error :(



1.2k
edited Sep '15

oh, I use version 1.30

sorry :(

Thx for your try :) I use version 2.0.7

I'm sorry, I need to sleep now I will try tomorrow which version did you use?

Tested but same, no update error :(



7.1k
Accepted
answer
edited Sep '15

Can you try to take off auto increment to see if it works? Also may be you can take off all foreign key contrains for starter.



3.4k

I change my CDM with MySQL WorkBench to remove primary keys same thing. Then I had same relations and It's work. Really weird problem.

Thx all guys for reply

Can you try to take off auto increment to see if it works? Also may be you can take off all foreign key contrains for starter.