CakePHP use wrong sequence name (PostgreSQL)

一笑奈何 提交于 2019-12-07 18:28:20

问题


When I try save some information in Postgres table, CakePHP return this error for me:

array(
    (int) 0 => '[PDOException] SQLSTATE[42P01]: Undefined table: 7 ERRO:  relação "public.cashier_transaction_transaction_num_seq" não existe
Request URL: /www/cashiers/open
Stack Trace:

But CakePHP is correctly: this sequence doesn't exist. The correct sequence is transaction_num_seq.

How can I change that IN CAKE (I can't change the database).


回答1:


There seems to be an undocumented, optional attribute used by the describe($model) function in Postgres.php

if (isset($model->sequence)) {
    $this->_sequenceMap[$table][$model->primaryKey] = $model->sequence;
}

Use it, like so, to specify the sequence of the table's primary key.

public class YourModel extends AppModel {
    public $sequence = 'public.foobar_seq';
    ...
}



回答2:


The only way I found to fix this error, was creating an action in each model named "nextval ()" for example, and run the default PostgreSQL query to get the next sequence.

public function nextval() {
    $sql = "select nextval('transaction_num_seq') as nextval";
    $result = $this->query($sql);
    return $result[0][0]['nextval'];
}


来源:https://stackoverflow.com/questions/14965410/cakephp-use-wrong-sequence-name-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!