cakephp模型外键不工作

时间:2014-05-05 12:02:30

标签: php cakephp-2.4

我有两张桌子:

-> model_tickets 
CREATE TABLE IF NOT EXISTS `detail_models` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `ModelTicket_id` int(11) NOT NULL,
  `nom` varchar(32) NOT NULL,
  `text` varchar(32) NOT NULL,
  `taille` enum('8','9','10','11','12','13','14','15','16','17','18','19','20') NOT NULL,
  `police` enum('Impact','courier new','times new roman','comic sans ms') NOT NULL,
  `bold` int(2) NOT NULL,
  `italic` int(2) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `ModelTicket_id` (`ModelTicket_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=53 ;

-> detail_models
CREATE TABLE IF NOT EXISTS `model_tickets` (
  `ModelTicket_id` int(11) NOT NULL AUTO_INCREMENT,
  `nom_model` varchar(32) NOT NULL,
  `is_active` tinyint(1) NOT NULL,
  `date_modif` date NOT NULL,
  PRIMARY KEY (`ModelTicket_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

ModelTicket.php模型中的代码:

var $hasMany = array('DetailModel' =>
    array('className' => 'DetailModel',
        'conditions' => '',
        //'order' => 'Comment.created DESC',
        'limit' => '5',
        'foreignKey' => 'ModelTicket_id',
        'dependent' => true,
        'exclusive' => false,
        'finderQuery'   => '',
        'fields' => '',
        'offset' => '',
        'counterQuery' => ''
    )
);

错误显示:

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'ModelTicket.id' in 'field list'

任何想法可能是什么问题?

2 个答案:

答案 0 :(得分:0)

我不是CAKEPHP的专家,但如果你的表是model_tickets,你的模型不应该被命名为" Model_Ticket.php"而不是ModelTicket.php?

答案 1 :(得分:0)

您的ModelTicket.php没有遵循如何命名主键的标准。它应该被称为" id"。

如果您无法更改列名称,则可以修改模型并让其知道您的主键不是" id"但是" ModelTicket_id"

public $primaryKey = 'ModelTicket_id';

更多信息here