连接表中的HABTM附加字段

时间:2011-11-20 15:30:10

标签: php cakephp cakephp-2.0

我在HABTM协会方面遇到了一些麻烦。

我要做的是以下内容:

模板HABTM Medium 页HABTM中等

为此,我创建了联接表object_media,并希望它包含object_idmedium_idmodel字段。页面ID或模板ID将进入object_id字段,模型将具有“页面”或“模板”。通过这样做,我将能够使用我的中型模型连接任何模型。 (媒体=媒体,所有图像,视频,文档等都保存在那里)。然后我可以将条件传递给HABTM关系,以获得该特定模型的媒体。例如:ObjectMedium.model => 'Template'将返回连接到模板的所有媒体。

简而言之:我想要一张将任何模型链接到任何媒体的表格。

的template.php

class Template extends AppModel{
    public $name = 'Template';

    public $hasMany = array(
        'TemplateColumn'
    );

    public $hasAndBelongsToMany = array(
        'Medium' => array(
        'className' => 'Medium',
        'joinTable' => 'object_media',
        'foreignKey' => 'object_id',
        'associationForeignKey' => 'medium_id'
        )
    );
}

Medium.php

class Medium extends AppModel{
    public $name = 'Medium';
    public $useTable = 'media';

    public $hasAndBelongsToMany = array(
        'Template' => array(
        'className' => 'Template',
        'joinTable' => 'object_media',
        'foreignKey' => 'medium_id',
        'associationForeignKey' => 'object_id'
        )
    );
 }

然而,给它提供以下数据甚至不会保存介质:

 Array
    (
[Template] => Array
    (
        [name] => 
    )

[TemplateColumn] => Array
    (
        [0] => Array
            (
                [block] => 0
                [column] => 0
                [grid] => 7
            )

        [1] => Array
            (
                [block] => 0
                [column] => 1
                [grid] => 5
            )

        [2] => Array
            (
                [block] => 1
                [column] => 0
                [grid] => 6
            )

        [3] => Array
            (
                [block] => 1
                [column] => 1
                [grid] => 6
            )

    )

[Medium] => Array
    (
        [0] => Array
            (
                [name] => 7917196b42c3626c10ab024bbafb8171
                [src] => 134a16c2202ff2b0c9b1c51dddb1bcfc.jpg
                [type] => Image
            )

    )

这给了我以下查询:

INSERT INTO `templates` (`name`, `modified`, `created`) VALUES ('', '2011-11-20 15:18:46', '2011-11-20 15:18:46')
SELECT `ObjectMedium`.`medium_id` FROM `object_media` AS `ObjectMedium` WHERE `ObjectMedium`.`object_id` = 30
INSERT INTO `template_columns` (`block`, `column`, `grid`, `template_id`, `modified`, `created`) VALUES (0, 0, 7, 30, '2011-11-20 15:18:46', '2011-11-20 15:18:46')
INSERT INTO `template_columns` (`block`, `column`, `grid`, `template_id`, `modified`, `created`) VALUES (0, 1, 5, 30, '2011-11-20 15:18:46', '2011-11-20 15:18:46')
INSERT INTO `template_columns` (`block`, `column`, `grid`, `template_id`, `modified`, `created`) VALUES (1, 0, 6, 30, '2011-11-20 15:18:46', '2011-11-20 15:18:46')
INSERT INTO `template_columns` (`block`, `column`, `grid`, `template_id`, `modified`, `created`) VALUES (1, 1, 6, 30, '2011-11-20 15:18:46', '2011-11-20 15:18:46')

任何出错的想法?以及如何在连接表中添加模型名称?

1 个答案:

答案 0 :(得分:0)

还要看一下这个相同主题的不同平台上的许多答案: http://www.google.de/search?gcx=c&ix=c2&sourceid=chrome&ie=UTF-8&q=habtm+extra+join+table

这可能是最有帮助的: {{3P>