【问题标题】:cakephp hasMany gives empty data setcakephp hasMany 给出空数据集
【发布时间】:2011-01-13 05:58:13
【问题描述】:

我有一个“电机”模型:

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 个结果。

谁能指出我做错了什么或者只是简单地省略了?

谢谢!

【问题讨论】:

  • 发布你的控制器查找操作的代码

标签: php cakephp cakephp-1.3


【解决方案1】:

我在您的电机模型中看到 Moter.id = 0000000001,在您的 MotorPicture 中看到 MotorPicture.moder_id=1

00000000011不一样

【讨论】:

    【解决方案2】:

    这类事情通常是由过于严格的递归设置引起的。在调用 $Motor->find 之前,尝试在控制器中设置 $Motor->recursive = 1。

    【讨论】:

      【解决方案3】:

      事实证明,我的键和外键完全不匹配。一个是 int (10),另一个是 int (11)。一旦我在我的数据库中进行了一个匹配,它就开始工作了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多