【问题标题】:CakePHP using foreign keys with different names as the associated primary keysCakePHP 使用不同名称的外键作为关联的主键
【发布时间】:2014-07-04 04:51:24
【问题描述】:

我设置了多个模型,我想将它们与这样的主模型关联:

class CommonType extends AppModel {
    public $useDbConfig = 'common';

    public $hasOne = array(
        'CommonTypeDescription' => array(
            'className' => 'CommonTypeDescription',
            'foreignKey' => 'type_description_id',
            'dependent' => true
        ),
        'CommonTypeExperience' => array(
            'className' => 'CommonTypeExperience',
            'foreignKey' => 'type_expirience_id',
            'dependent' => true             
        ),
        'CommonTypeProperty' => array(
            'className' => 'CommonTypeProperty',
            'foreignKey' => 'type_property_id',
            'dependent' => true         
        ),
    );

    public $belongsTo = array(
        'CommonTypeCategory' => array(
            'className' => 'CommonTypeCategory',
            'foreignKey' => 'common_type_id',
    ));

}

在 Heidi SQL(例如)中,我为表 type_experiences、type_properties 和 type_descriptions 设置外键,以便我的代码和表中的外键具有相同的名称。

但是现在,我不想将主键命名为外键,(例如)type_descriptions 表的主键应该命名为“id”。这是type_description的模型:

class CommonTypeDescription extends AppModel {
   public $name = 'CommonTypeDescription';
   public $useDbConfig = 'common';
   public $useTable = 'type_descriptions';
   public $primaryKey = 'id';

}

这里是数据库创建代码:

CREATE TABLE `type_descriptions` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`common_type_id` INT(11) NULL DEFAULT NULL,
`short_description` VARCHAR(50) NULL DEFAULT NULL,
`long_description` VARCHAR(50) NULL DEFAULT NULL,
`notes` VARCHAR(50) NULL DEFAULT NULL,
`modified` DATETIME NULL DEFAULT NULL,
`created` DATETIME NULL DEFAULT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `type_description_id` FOREIGN KEY (`common_type_id`) REFERENCES `common_types` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION) ...

但是当我尝试从我的模型 CommonType ($this->find('all')) 获取数据时,我收到类似“Unknown column 'CommonTypeDescription.type_description_id' in 'on clause' 的错误强>”

首先,我认为我必须将描述模型的主键声明为“id”,但它接缝的是,外键定义覆盖了关联模型中主键蛋糕的命名约定。

非常感谢您的帮助和问候, 圭多

ps。我已经更改了这篇文章的模型名称(如果有类型错误)。因此,当我重命名关联表的主键时,一切正常,但我希望它们只命名为“id”。

【问题讨论】:

    标签: cakephp naming convention


    【解决方案1】:

    如何将您的 CommonType 模型更新为:

       'CommonTypeDescription' => array(
            'className' => 'CommonTypeDescription',
            'foreignKey' => 'common_type_id', //or try type_description_id if there's an error
            'dependent' => true
        )
    

    然后在你的 CommonTypeDescription 模型上添加这个关系,我想你忘了添加属于 CommonType 模型的关系

        public $belongsTo = array(
            'CommonType' => array(
            'className' => 'CommonType ',
            'foreignKey' => 'common_type_id',
        ));
    

    【讨论】:

    • 谢谢,这个解决方案暴露了我的错误。我认为 Cake 的外键与数据库中的外键是相互依赖的。现在,我在两个模型中都添加了依赖项,并使用 cakeconventions 定义了外键。它有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    相关资源
    最近更新 更多