我在模型中遇到HABTM关系问题。我在这个网站和其他许多网站上搜索了一些示例和参考资料,但却没有成功。有条不紊的帖子HABTM标签官方文档效果很好,但推断到我的模型失败了。找到的所有信息都不包含复合主键的示例或引用(我怀疑由于复合主键,关系不起作用,我不能更改此模式)
我的目标是获得关系:
Customer HABTM Products.
所以,用
$ customers = $ this-> Customer-> find('all');
获取Customer对象中与客户相关的所有产品的数组。
目前我无法解决此问题: $ customer 没有指定的产品数组。
我感谢任何帮助或参考,以帮助我解决这个问题。
这是我的数据库:
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);
}
答案 0 :(得分:1)
您的HABTM表应为customers_products
(不是customer_products)。这可能会解决它。请记住格式是按字母顺序复数。因此,如果它是客户HABTM产品,那么它就是customers_products。如果是用户HABTM产品,那将是products_users。
<强>更新强>
如果您希望HABTM记录显示在索引中,您还需要删除:
$this->Customer->recursive = 0;
这将阻止相关记录出现。