【发布时间】:2012-03-15 19:23:00
【问题描述】:
我的模型中的 HABTM 关系有问题。我在这个网站和许多其他网站上搜索了几个星期没有成功的例子和参考资料。古典 Posts HABTM Tags 官方文档运行良好,但外推到我的模型失败。找到的所有信息都不包含复合主键的示例或参考(我怀疑这种关系由于复合主键而不起作用,我无法更改此架构)
我的目标是获得关系:
Customer HABTM Products.
所以,与
$customers = $this->Customer->find('all');
在 Customer 对象中获取与客户相关的所有产品的数组。
目前我无法让它工作:$customer 没有指定的 Products 数组。
感谢任何帮助或参考来帮助我解决此问题。
这是我的数据库:
CREATE TABLE `customers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`lang` varchar(2) NOT NULL,
`name` varchar(145) NOT NULL,
`logo` varchar(145) NOT NULL,
`description` text,
PRIMARY KEY (`id`,`lang`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8
CREATE TABLE `products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`lang` varchar(2) NOT NULL,
`category_id` int(11) NOT NULL,
`service_id` int(11) NOT NULL,
`description` text,
`picture` varchar(145) NOT NULL,
PRIMARY KEY (`id`,`category_id`,`service_id`,`lang`),
KEY `fk_products_services1` (`service_id`,`lang`),
KEY `fk_products_categories1` (`category_id`,`lang`),
CONSTRAINT `fk_products_categories1` FOREIGN KEY (`category_id`, `lang`) REFERENCES `categories` (`id`, `lang`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_products_services1` FOREIGN KEY (`service_id`, `lang`) REFERENCES `services` (`id`, `lang`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8
CREATE TABLE `customer_products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`customer_id` int(11) NOT NULL,
`product_id` int(11) NOT NULL,
PRIMARY KEY (`id`,`customer_id`,`product_id`),
KEY `fk_customer_products_products1` (`product_id`),
KEY `fk_customer_products_customers` (`customer_id`),
CONSTRAINT `fk_customer_products_customers` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_customer_products_products1` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
这是我的客户模型:
<?php
App::uses('AppModel', 'Model');
/**
* Customer Model
*
* @property CustomerProduct $CustomerProduct
* @property Product $AllProducts
*/
class Customer extends AppModel {
/**
* Display field
*
* @var string
*/
public $displayField = 'name';
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* hasAndBelongsToMany associations
*
* @var array
*/
public $hasAndBelongsToMany = array(
'Products' => array(
'className' => 'Product',
'joinTable' => 'customer_products',
'foreignKey' => 'customer_id',
'associationForeignKey' => 'product_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
}
这是我的索引客户控制器方法:
public function index() {
$this->layout = 'header_chico';
$this->Customer->recursive = 0;
$customers = $this->Customer->find('all');
$this->set('customers', $customers);
}
【问题讨论】:
标签: cakephp has-and-belongs-to-many composite-key composite-primary-key