我在模型中遇到了HABTM關係的問題。我在這個網站上搜索了很多例子和參考資料,並且在幾個星期內沒有成功。該類文章文章HABTM標籤官方文檔效果很好,但外推到我的模型失敗。發現的所有信息不包含複合主鍵的實例或引用(我懷疑是因爲複合主鍵的關係將無法正常工作,我不能老是改變這種模式)CakePHP:HABTM和遞歸屬性獲取所有相關數據
我的目標是讓關係:
Customer HABTM Products.
所以,用
$客戶= $這個 - >客戶 - >找到( '所有');
獲取客戶對象中與客戶相關的所有產品的數組。
目前我不能得到這個工作:$客戶沒有spected 產品陣列。
我感謝任何幫助或引用來幫助我解決這個問題。
這是我的分貝:
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);
}
謝謝@Chuck。我在我的測試環境中嘗試過,但沒有成功。我還沒有產品陣列。無論如何,我無法在生產服務器中更改數據庫模式(這就是爲什麼它在CustomerController類的joinTable屬性中指定爲「customer_products」)的原因。 – Pablushka 2012-02-25 20:25:39
我剛剛意識到你正在尋找索引方法中的相關記錄。這是阻礙你的遞歸。看到我上面的更新。 – 2012-02-25 20:52:21
就是這樣。非常感謝@Chuck。我會改變主題來反映真正的問題。 – Pablushka 2012-02-26 21:52:42