1

我在模型中遇到了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); 
} 

回答

1

您的HABTM表应该是customers_products(不是customer_products)。这可能会解决它。请记住,格式是按字母顺序排列的复数形式。所以如果它是客户HABTM产品,它就是customers_products。如果它是用户HABTM产品,它将是products_users。

UPDATE

如果你想在HABTM记录出现在索引中,你还需要删除:

$this->Customer->recursive = 0; 

这将防止出现相关记录。

+0

谢谢@Chuck。我在我的测试环境中尝试过,但没有成功。我还没有产品阵列。无论如何,我无法在生产服务器中更改数据库模式(这就是为什么它在CustomerController类的joinTable属性中指定为“customer_products”)的原因。 – Pablushka 2012-02-25 20:25:39

+0

我刚刚意识到你正在寻找索引方法中的相关记录。这是阻碍你的递归。看到我上面的更新。 – 2012-02-25 20:52:21

+0

就是这样。非常感谢@Chuck。我会改变主题来反映真正的问题。 – Pablushka 2012-02-26 21:52:42