繁体   English   中英

Cakephp无法在插入时自动生成记录ID

[英]Cakephp is failing to auto generate record id on insert

我在cake php中有一个非常简单的加入模型。 我遇到一个错误,其中每个插入的新记录的ID为“”。

 <?php


 App::uses('AppModel', 'Model');
/**
 * Subscriber Model
 *
 * @property ChallengeMember $ChallengeMember
 * @property Activity $Activity
 */
class Subscriber extends AppModel {


    //The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
    public $belongsTo = array(
        'ChallengeMember' => array(
            'className' => 'ChallengeMember',
            'foreignKey' => 'challenge_member_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Activity' => array(
            'className' => 'Activity',
            'foreignKey' => 'activity_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );
}

该表如下所示:

  public $fields = array(
        'id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 36, 'key' => 'primary', 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'),
        'activity_id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 37, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'),
        'challenge_member_id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 37, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'),
        'indexes' => array(

        ),
        'tableParameters' => array('charset' => 'latin1', 'collate' => 'latin1_swedish_ci', 'engine' => 'InnoDB')
    );

测试时

$this->Subscriber->create(array('challenge_member_id' => 'test123456','ChallengeMember.first_time_in_dash' => '1'));
$this->Subscriber->save();
Debugger::dump($this->Subscriber->find('all'));

输出

array(
        'Subscriber' => array(
            'id' => '',
            'challenge_member_id' => 'test123456',
            'activity_id' => '',
            'created' => '2014-07-16 18:45:14'
        )
        ........

如您所见,cake太无法自动生成ID,这对于我的其他模型来说是不正确的。 有任何想法吗 ? 我重新烘焙了模型和测试装置,但没有喜悦!

好的,所以我从来没有深入了解这个问题,问题似乎只在于名为“ Subscriber”的表。 表中的每个其他表都有一个ID字段,定义为:

'id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 36, 'key' => 'primary', 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1')

在Model :: save()上,cakephp自动生成一个ID,如下所示:

53c4fe0c-c38c-4915-a056-111fd1f54a2f.

最后,我只是将订户ID字段设置为“ INT(11)Auto Increment”,让mysql负责密钥生成。 这解决了问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM