我有一个“马达”型号:
class Motor extends AppModel
{
var $name = 'Motor';
var $hasMany = array(
'MotorPictures' => array(
'className' => 'MotorPicture',
'foreignKey' => 'motor_id',
'dependent' => true
)
);
}
与MotorPicture模型一起:
class MotorPicture extends AppModel
{
var $name = 'MotorPicture';
var $actsAs = array(
'FileUpload.FileUpload' => array
(
'uploadDir' => 'img/motors',
'required' => true
)
);
}
这是数据库:
--
-- Table structure for table `motors`
--
CREATE TABLE IF NOT EXISTS `motors` (
`id` int(10) unsigned zerofill NOT NULL AUTO_INCREMENT,
`make` varchar(64) NOT NULL,
`model` varchar(64) NOT NULL,
`year` year(4) NOT NULL,
`notes` text NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`edited` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ;
--
-- Dumping data for table `motors`
--
INSERT INTO `motors` (`id`, `make`, `model`, `year`, `notes`, `created`, `edited`)
VALUES (0000000001, 'Audi', 'S4', 2000, 'This is a freaking sweet car.', '2011-01-11 21:04:00', '0000-00-00 00:00:00');
-- --------------------------------------------------------
--
-- Table structure for table `motor_pictures`
--
CREATE TABLE IF NOT EXISTS `motor_pictures` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`motor_id` int(11) unsigned NOT NULL,
`name` varchar(256) NOT NULL,
`type` varchar(256) NOT NULL,
`size` int(11) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `motor_pictures`
--
INSERT INTO `motor_pictures` (`id`, `motor_id`, `name`, `type`, `size`, `created`, `modified`)
VALUES (1, 1, 'kitten.JPG', 'image/jpeg', 80518, '2011-01-12 20:13:55', '2011-01-12 20:13:55'),
(2, 1, 'kitten-1.JPG', 'image/jpeg', 80518, '2011-01-12 20:15:28', '2011-01-12 20:15:28');
当我调出我的视图和print_r($ motor)时,输出是这样的:
Array ( [Motor] => Array ( [id] => 0000000001 [make] => Audi [model] => S4 [year] => 2000 [notes] => This is a freaking sweet car. [created] => 2011-01-11 21:04:00 [edited] => 0000-00-00 00:00:00 ) [MotorPictures] => Array ( ) )
正如您所看到的那样(在那个漫长的事情的最后),“MotorPictures”数组为空,即使hasMany关系强制执行的SELECT返回2导致调试输出。
有人能指出我做错了什么或者只是简单地省略了吗?
谢谢!
答案 0 :(得分:0)
我在你的马达模型Moter.id = 0000000001
和MotorPicture
中看到了MotorPicture.moder_id=1
。
0000000001
与1
答案 1 :(得分:0)
这种事情通常是由过于严格的递归设置引起的。在调用$ Motor-> find之前,尝试在控制器中设置$ Motor-> recursive = 1。
答案 2 :(得分:0)
事实证明我的密钥和外键没有 完全 匹配。一个是int(10),另一个是int(11)。一旦我在我的数据库中与另一个匹配,它就开始工作了。