cakephp hasMany дает пустой набор данных - PullRequest
0 голосов
/ 13 января 2011

У меня есть модель "Мотор":

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" пуст, хотя SELECT, который принудительно устанавливает отношение hasMany, возвращает 2 результата в результате отладки.

Может ли кто-нибудь указать мне на то, что я делаю неправильно или просто опущу?

Спасибо!

Ответы [ 3 ]

0 голосов
/ 15 января 2011

Часто такие вещи вызваны слишком рекурсивными настройками.Попробуйте установить $ Motor-> recursive = 1 в вашем контроллере, прежде чем вызывать $ Motor-> find.

0 голосов
/ 24 января 2011

Как оказалось, мой ключ и внешний ключ не точно совпадаютОдин был int (10), а другой - int (11).Как только я сделал одно совпадение с другим в моей базе данных, все это заработало.

0 голосов
/ 15 января 2011

Я вижу в вашей модели двигателя Moter.id = 0000000001, а в вашей MotorPicture у вас есть MotorPicture.moder_id=1.

0000000001 не совпадает с 1

...