【问题标题】:CakePHP: HABTM and Recursive attribute to get all related dataCakePHP:HABTM 和递归属性来获取所有相关数据
【发布时间】: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


    【解决方案1】:

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

    更新

    如果您希望 HABTM 记录出现在索引中,您还需要删除:

    $this->Customer->recursive = 0;
    

    这将阻止相关记录出现。

    【讨论】:

    • 谢谢@Chuck。我在我的测试环境中尝试过,但没有成功。我还没有 Products 数组。无论如何,我无法在生产服务器中更改我的数据库模式(这就是为什么它在 CustomerController 类的 joinTable 属性中指定为“customer_products”)。
    • 我刚刚意识到您正在查找索引方法中的相关记录。阻碍你的是递归。请参阅上面的更新。
    • 就是这样。非常感谢@Chuck。我将改变主题以反映真正的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 2019-09-11
    • 2015-08-16
    相关资源
    最近更新 更多