帮助CakePHP:我无法烘烤

时间:2011-04-22 18:18:32

标签: cakephp-1.3 relationship has-many

我是cakephp的新手并且有问题。我在mysql中创建了2个表 我试图创建的关系是“category_products”有很多“产品”。

CREATE TABLE category_products (  
  id int NOT NULL PRIMARY KEY,  
  name varchar(30) NOT NULL  
) ENGINE=InnoDB;  

CREATE TABLE products (  
  id int NOT NULL PRIMARY KEY,  
  name varchar(30) NOT NULL,  
  category_product_id int NOT NULL,  
  FOREIGN KEY (category_product_id) REFERENCES category_products(id)  
) ENGINE=InnoDB; 

当我烘焙'category_products'时一切都很好,但是当我在控制台中烘焙'产品'时,我明白了:

=============================================================   
Possible Models based on your current database:  
1. CategoryProduct  
2. Product  
Enter a number from the list above,type in the name of another model, or 'q' to exit    
[q] > 2  

Baking model class for Product...  
Creating file /opt/lampp/htdocs/caketest/app/models/product.php  
Wrote `/opt/lampp/htdocs/caketest/app/models/product.php`  
Product Model was baked.  
SimpleTest is not installed. Do you want to bake unit test files anyway? (y/n)   [y] > n  

**Error: Missing database table 'categories' for model 'Category'**

============================================================  
Cakephp **doesn't create the view** and when i open the **model** for product.php and i see this at then end.  

var $belongsTo = array(  
        'CategoryProduct' => array(  
            'className' => 'CategoryProduct',  
            'foreignKey' => 'category_product_id',  
            'conditions' => '',  
            'fields' => '',  
            'order' => ''  
        )  
    );  

var $hasAndBelongsToMany = array(  
        'Category' => array(  
            'className' => 'Category',  
            'joinTable' => 'category_products',  
            'foreignKey' => 'product_id',  
            'associationForeignKey' => 'category_id',  
            'unique' => true,  
            'conditions' => '',  
            'fields' => '',  
            'order' => '',  
            'limit' => '',  
            'offset' => '',  
            'finderQuery' => '',  
            'deleteQuery' => '',  
            'insertQuery' => ''  
        )  
    );  

我的问题是我做错了什么?显然,cakephp认为我正在努力创造一个 关系HABTM。我正在使用cakephp 1.3.7

请帮帮我。谢谢!

1 个答案:

答案 0 :(得分:0)

这就是烘焙的设计方式。你没有做错任何事情,只是你在数据库中有产品,所以它认为category_products是HABTM。如果您希望它正常工作,您可以选择以下几个选项:

1-在烘焙category_products之前删除产品表,然后手动将关系添加到category_products。

2-在烘焙过程中将产品表更改为其他内容,然后返回模型并更新名称和与产品的关系。

3-忽略您所获得的错误,并从模型中删除HABTM关系。

快乐的编码!