2013-10-11 213 views
0

我有幾個表格:ingredients,customers,recipes,menusrestaurantsCakePHP HABTM關係

的條件是:ingredients HABTM customersrecipes HABTM ingredientsmenus的hasMany recipesrestaurants的hasMany menus

但是,當我嘗試使用cmd控制檯烘烤所有食物時,自動創建的關係就像成分hasmany customers_ingredients,客戶hasmany customers_ingredients和customer_ingredients屬於客戶和成分,而不是提供成分hasandbelongtomany客戶。哪裏不對?表格定義或控制檯?

這裏我列出我創建表的語句:

CREATE TABLE customers (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
    cust_gname VARCHAR(20) NOT NULL, 
    cust_fname VARCHAR(30) NOT NULL, 
    cust_street VARCHAR(30) NOT NULL, 
    cust_suburb VARCHAR(30) NOT NULL, 
    cust_state VARCHAR(6) NOT NULL, 
    cust_postcode VARCHAR(4) NOT NULL, 
    cust_email VARCHAR(50) NOT NULL, 
    cust_phone VARCHAR(12), 
    cust_mobile VARCHAR(12) 
); 

CREATE TABLE restaurants (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
    name VARCHAR(30), 
    address1 VARCHAR(30), 
    address2 VARCHAR(30), 
    suburb VARCHAR(30), 
    state VARCHAR(10), 
    postcode VARCHAR(4) 
); 

CREATE TABLE ingredients (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
    name VARCHAR(30) 
); 

CREATE TABLE menus (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
    description VARCHAR(30), 
    restaurant_id INT UNSIGNED, 
    CONSTRAINT fk_restID FOREIGN KEY (restaurant_id) REFERENCES restaurants(id) 
); 

CREATE TABLE recipes (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
    name VARCHAR(30), 
    menu_id INT UNSIGNED, 
    image VARCHAR(30), 
    CONSTRAINT fk_menuID FOREIGN KEY (menu_id) REFERENCES menus(id) 
); 

CREATE TABLE ingredient_recipes (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
    ingredient_id INT UNSIGNED, 
    recipe_id INT UNSIGNED, 
    CONSTRAINT fk_ingID FOREIGN KEY (ingredient_id) REFERENCES ingredients(id), 
    CONSTRAINT fk_recID FOREIGN KEY (recipe_id) REFERENCES recipes(id) 
); 

CREATE TABLE customer_ingredients (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
    customer_id INT UNSIGNED, 
    ingredient_id INT UNSIGNED, 
    CONSTRAINT fk_ingrID FOREIGN KEY (ingredient_id) REFERENCES ingredients(id), 
    CONSTRAINT fk_cusID FOREIGN KEY (customer_id) REFERENCES customers(id) 
); 

回答

1

爲HABTM關係的名字慣例希望這兩個名字是複數:customers_ingredientsingredients_recipes

+0

這解決了問題。非常感謝! –