2

I have a old database in PostgreSQL with multiple primary key.

When I try save any information in these tables, an error occurred because CakePHP trying to get the lastInsertId. Because, as we know, CakePHP doesn't support multiple primary key.

So, I wanna know, how can I disable this functionality/option?

I tried this, but doesn't work as expected.

$this->OrderDrinkBase->saveAll(
    $drinkBases,
    array('callbacks' => false, 'validate' => false)
);

The solution above, works, so I approved the answer. But, I really want a explanation of how can I disable the function lastInsertId in CakePHP in some cases.

4

1 回答 1

4

使用以下代码

class GroupToUser extends AppModel {

    var $name = 'GroupToUser';
    var $useTable = 'groups_users';

    var $primaryKeyArray = array('user_id','group_id');

    function exists($reset = false) {
        if (!empty($this->__exists) && $reset !== true) {
            return $this->__exists;
        }
        $conditions = array();
        foreach ($this->primaryKeyArray as $pk) {
            if (isset($this->data[$this->alias][$pk]) && $this->data[$this->alias][$pk]) {
                $conditions[$this->alias.'.'.$pk] = $this->data[$this->alias][$pk];
            }
            else {
                $conditions[$this->alias.'.'.$pk] = 0;
            }
        }
        $query = array('conditions' => $conditions, 'fields' => array($this->alias.'.'.$this->primaryKey), 'recursive' => -1, 'callbacks' => false);
        if (is_array($reset)) {
            $query = array_merge($query, $reset);
        }
        if ($exists = $this->find('first', $query)) {
            $this->__exists = 1;
            $this->id = $exists[$this->alias][$this->primaryKey];
            return true;
        }
        else {
            return parent::exists($reset);
        }
    }

}

欲了解更多信息,请查看这里http://mrphp.com.au/blog/multiple-primary-keys-cakephp#.UhxzGD90klQ

或者这个http://miljenkobarbir.com/using-multiple-column-primary-key-in-cakephp-cascade-delete-problem/

于 2013-08-27T09:41:19.057 回答