CakePHP2中的多态关联

时间:2012-10-11 08:29:29

标签: cakephp polymorphic-associations

我有3个模型,PageCourseContent

PageCourse包含元数据,Content包含HTML内容。

PageCourse都有多个Content

Content belongsTo PageCourse

为了避免page_id中有course_idContent字段(因为我希望这可以缩放到超过2个模型),我正在考虑使用多态关联。我开始使用面包店中的Polymorphic Behavior,但它正在为我的喜好生成太多的SQL查询,并且还抛出了“非法偏移”错误,我不知道如何修复(它是在2008年写的)似乎没有人最近提到它,所以错误可能是由于它没有为Cake 2设计的?)

无论如何,我发现我几乎可以通过硬编码模型中的关联来做我需要的一切:

页面模型

CREATE TABLE `pages` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `slug` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `created` datetime NOT NULL,
  `updated` datetime NOT NULL,
  PRIMARY KEY (`id`)
)

<?php 
class Page extends AppModel {
    var $name = 'Page';
    var $hasMany = array(
        'Content' => array(  
            'className' => 'Content',
            'foreignKey' => 'foreign_id',
            'conditions' => array('Content.class' => 'Page'),
        )
    );
}
?>

课程模型

CREATE TABLE `courses` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `slug` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `created` datetime NOT NULL,
  `updated` datetime NOT NULL,
  PRIMARY KEY (`id`)
)

<?php 
class Course extends AppModel {
    var $name = 'Course';
    var $hasMany = array(
        'Content' => array(  
            'className' => 'Content',
            'foreignKey' => 'foreign_id',
            'conditions' => array('Content.class' => 'Course'),
        )
    );
}
?>

内容模型

CREATE TABLE IF NOT EXISTS `contents` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `class` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
  `foreign_id` int(11) unsigned NOT NULL,
  `title` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `content` text COLLATE utf8_unicode_ci NOT NULL,
  `created` datetime DEFAULT NULL,
  `modified` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
)

<?php 
class Content extends AppModel {
    var $name = 'Content';   
    var $belongsTo = array(
        'Page' => array(
            'foreignKey' => 'foreign_id',
            'conditions' => array('Content.class' => 'Page')
        ),
        'Course' => array(
            'foreignKey' => 'foreign_id',
            'conditions' => array('Content.class' => 'Course')
        )
    );
}
?> 

好处是$this->Content->find('first')只生成一个SQL查询而不是3(就像多态行为一样),但问题是返回的数据集包含两个belongsTo模型,而它应该只有真正返回存在的那个。以下是返回数据的外观:

array(
    'Content' => array(
        'id' => '1',
        'class' => 'Course',
        'foreign_id' => '1',
        'title' => 'something about this course',
        'content' => 'The content here',
        'created' => null,
        'modified' => null
    ),
    'Page' => array(
        'id' => null,
        'title' => null,
        'slug' => null,
        'created' => null,
        'updated' => null
    ),
    'Course' => array(
        'id' => '1',
        'title' => 'Course name',
        'slug' => 'name-of-the-course',
        'created' => '2012-10-11 00:00:00',
        'updated' => '2012-10-11 00:00:00'
    )
)

我只希望它返回PageCourse中的一个,具体取决于Content.class中指定的那个

更新:组合PageCourse模型似乎是解决此问题的明显方案,但我上面显示的模式仅用于此目的题。实际的模式在字段方面实际上是非常不同的,每个模式与其他模型的关联数也不同。

更新2

以下是运行$this->Content->find('first');

产生的查询
SELECT `Content`.`id`, `Content`.`class`, `Content`.`foreign_id`, `Content`.`title`,
`Content`.`slug`, `Content`.`content`, `Content`.`created`, `Content`.`modified`, 
`Page`.`id`, `Page`.`title`, `Page`.`slug`, `Page`.`created`, `Page`.`updated`, 
`Course`.`id`, `Course`.`title`, `Course`.`slug`, `Course`.`created`, 
`Course`.`updated` FROM `cakedb`.`contents` AS `Content` 
LEFT JOIN `cakedb`.`pages` AS `Page` ON 
(`Content`.`foreign_id` = `Page`.`id` AND `Content`.`class` = 'Page') 
LEFT JOIN `cakedb`.`courses` AS `Course` ON (`Content`.`foreign_id` = `Course`.`id` 
AND `Content`.`class` = 'Course') WHERE 1 = 1 LIMIT 1

1 个答案:

答案 0 :(得分:7)

您的查询没问题。只需在find:

后过滤空值
public function afterFind($results, $primary = false) {
    return Set::filter($results, true);
}

查看this问题。

您还可以尝试向conditions find Page.id提出Course.id非空 {{1}}不为空的查询。