CakePHP中具有直接多态关联的MySQL错误

Posted

技术标签:

【中文标题】CakePHP中具有直接多态关联的MySQL错误【英文标题】:MySQL Error with straightforward polymorphic association in CakePHP 【发布时间】:2013-09-23 07:47:20 【问题描述】:

我有 4 个模型,ApplicationAccommodationInvoiceInvoiceItem

InvoiceApplicationAccommodation都有很多InvoiceItem

InvoiceItem 属于ApplicationAccommodationInvoice

所有这些的目的是使Invoice(来自InvoiceItem 模型)的行项目可以通过InvoiceItem.foreign_idAccommodationApplication 相关联,具体取决于InvoiceItem.class是设置为Accommodation还是Application

据我了解,这称为多态关联。

不幸的是,当我执行 $this->Invoice->find('all'); 并将递归设置为 3(或设置为 -1 并在可包含中指定的适当字段)时,我收到此 mysql 错误:

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 
'InvoiceItem.class' in 'where clause'

SQL Query: SELECT `Application`.`id`, `Application`.`name`, 
`Application`.`created`, `Application`.`updated` FROM `applications` 
AS `Application` WHERE `Application`.`id` = 3786 AND 
`InvoiceItem`.`class` = 'Application' 

我不明白为什么 Cake 会尝试添加 InvoiceItem.class 作为条件而不为 InvoiceItem 模型创建连接。

这是我的模型(注意:为了便于阅读,我已经减少了每个模型中的字段数量——ApplicationAccommodation 模型在现实中很少有相似的字段):

住宿模式

CREATE TABLE `accommodations` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `created` datetime NOT NULL,
  `updated` datetime NOT NULL,
  PRIMARY KEY (`id`)
)

<?php 
class Accommodation extends AppModel 
    var $name = 'Accommodation';
    var $hasMany = array(
        'InvoiceItem' => array(  
            'className' => 'InvoiceItem',
            'foreignKey' => 'foreign_id',
            'conditions' => array('InvoiceItem.class' => 'Accommodation'),
        )
    );

?>

应用模型

CREATE TABLE `applications` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `created` datetime NOT NULL,
  `updated` datetime NOT NULL,
  PRIMARY KEY (`id`)
)

<?php 
class Application extends AppModel 
    var $name = 'Application';
    var $hasMany = array(
        'InvoiceItem' => array(  
            'className' => 'InvoiceItem',
            'foreignKey' => 'foreign_id',
            'conditions' => array('InvoiceItem.class' => 'Application'),
        )
    );

?>

InvoiceItem 模型

CREATE TABLE IF NOT EXISTS `invoice_items` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `class` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
  `foreign_id` int(10) unsigned NOT NULL,
  `name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `created` datetime DEFAULT NULL,
  `modified` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
)

<?php 
class InvoiceItem extends AppModel 
    var $name = 'InvoiceItem';   
    var $belongsTo = array(
        'Accommodation' => array(
            'foreignKey' => 'foreign_id',
            'conditions' => array('InvoiceItem.class' => 'Accommodation')
        ),
        'Application' => array(
            'foreignKey' => 'foreign_id',
            'conditions' => array('InvoiceItem.class' => 'Application')
        ),
        'Invoice' => array(
            'className' => 'Invoice',
            'foreignKey' => 'invoice_id',
        ),
    );

?> 

发票模型

CREATE TABLE IF NOT EXISTS `invoices` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `created` datetime DEFAULT NULL,
  `modified` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
)

<?php 
class Invoice extends AppModel 
    var $name = 'Invoice';   
    var $hasMany = array(
        'InvoiceItem' => array(
            'className' => 'InvoiceItem'
            'foreignKey' => 'invoice_id'
        )
    );

?> 

我正在使用 CakePHP 2.4.0

【问题讨论】:

【参考方案1】:

InvoiceItem 类扩展了 AppModel,并且绑定了 nobelongsTo。

class InvoiceItem extends AppModel 
    var $name = 'InvoiceItem';   
    var $belongsTo = null

您可以在运行时根据内容为 InvoiceItem 分配一个合适的 belongsTo

$this->InvoiceItem->bindModel(array( 'belongsTo' => array(...) ) )

【讨论】:

以上是关于CakePHP中具有直接多态关联的MySQL错误的主要内容,如果未能解决你的问题,请参考以下文章

CakePHP 3 保存 BelongsToMany 关联未知类型“”错误

CakePHP 3 - 保存前的数据关联

CakePHP 3.x - 关联错误 - 无法将表链接在一起

具有多态实体的子类关联表的 SQLAlchemy 设置

在 CakePHP 3 中保存多个 belongsToMany 关联

基于具有多态关联的 paerpclip 中的模型生成不同的图像样式