dtgr3392 2014-07-16 16:54
浏览 48

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

I have a very simple join model in cake php. I'm encountering a bug where the ID for each new record inserted is "".

 <?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' => ''
        )
    );
}

This is what the table looks like:

  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')
    );

When testing,

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

Outputs

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

As you can see cake is failing too autogen the ID, which is out of character to the rest of my models. Any ideas ? I've re-baked the model and test fixtures but no joy!

  • 写回答

1条回答 默认 最新

  • dousi4257 2014-07-17 08:00
    关注

    Ok, so I never quite got to the bottom of this, the issue only seems to be with the table named "Subscriber". Every other table in has an ID field defined as:

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

    On Model::save(), cakephp auto generates an ID like the one below:

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

    In the end I just set the subscriber ID field to "INT(11) Auto Increment", letting mysql take care of key generation. This solved the issue.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度