1

I need to change the value of two rows in the order field of my database. This field is unique.

What I tried:

  1. Storing the value of both items in a PHP variable,

  2. Setting the first row's field value to NULL (nulls are accepted in the field),

  3. Setting the value of the second row's field to the value that was held on the first row,

  4. Setting the first row's field value to the original second row field value.


It doesn't work, since I am getting a "dupplicate entry" error when executing the order change. I can't seem to find out how to do this using CakePHP.

Here is the code that I have written (even though it's not functionnal):

if ($second_row) {
    $next = $second_row['Immeuble']['order'];
    $prev = $first_row['Immeuble']['order'];

    $this->Immeuble->id = $first_row_id;
    $this->Immeuble->saveField('order', 'null');


    $this->Immeuble->id = $second_row['Immeuble']['id'];
    $this->Immeuble->saveField('order', $prev);

    $this->Immeuble->id = $first_row_id;
    $this->Immeuble->saveField('order',$next);
}
4

2 回答 2

0

当然它的重复键

你定义了 id 两次

     $this->Immeuble->id = $first_row_id;

     $this->Immeuble->id = $second_row['Immeuble']['id'];

接着

     $this->Immeuble->id = $first_row_id;

尝试删除最后一行

于 2012-12-17T19:06:39.023 回答
0

我上面所说的代码是完全功能的。我只是颠倒了一些值,这会搞砸一切。如果其他人需要做类似的事情,我不会删除这个问题。

于 2012-12-18T13:18:12.327 回答