2011-04-22 73 views
1

我是新的cakephp,並有問題。我在mysql中創建了2個表格 我試圖創建的關係是'category_products'有許多'產品'。幫助CakePHP:我不能烤

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

請幫助我。謝謝!

回答

0

這是烘烤設計的工作原理。除了數據庫中包含產品以外,您沒有做任何錯誤,因此認爲category_products是HABTM。如果您希望它正常工作,您可以選擇以下幾種選擇:

1-在烘焙category_products之前刪除產品表,然後手動將關係添加到category_products中。

2-在烘焙期間將產品表更改爲其他內容,然後返回模型並更新產品的名稱和關係。

3-忽略您收到的錯誤並從模型中刪除HABTM關係。

快樂編碼!