0

我有 4 个模型,, Application,和AccommodationInvoiceInvoiceItem

Invoice,Application并且Accommodation都有很多InvoiceItem

InvoiceItem属于ToApplicationAccommodationInvoice

所有这一切的目的是使 an 的行项目Invoice(来自InvoiceItem模型)可以与 anAccommodation或 an相关联ApplicationInvoiceItem.foreign_id具体取决于是否InvoiceItem.class设置为AccommodationorApplication

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

不幸的是,当我将$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'),
        )
    );
}
?>

发票项目模型

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

4

1 回答 1

0

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

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

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

$this->InvoiceItem->bindModel(array( 'belongsTo' => array(...) ) )
于 2013-09-24T16:02:36.127 回答