CakePHP 帮助:我不会烘焙
我是 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”时一切都很好,但是当我在控制台中烘焙“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
请帮助我。谢谢!
i am new with cakephp and have a problem. I have created 2 tables in mysql
The relation i'm trying to create is 'category_products' has many '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;
When i bake 'category_products' is all fine, but when i bake 'products' in console, i get this:
=============================================================
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' => ''
)
);
My question is what am i doing wrong??? apparently cakephp thinks i'm trying create a
relation HABTM. I'm using cakephp 1.3.7
please help me. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是烘焙的设计原理。除了数据库中有产品之外,您没有做任何错误,因此它认为category_products 是HABTM。如果您希望它正常工作,您有几个选项:
1- 在烘焙category_products 之前删除产品表,然后手动将关系添加到category_products 中。
2-在烘焙过程中将产品表更改为其他内容,然后返回模型并更新与产品的名称和关系。
3- 忽略您收到的错误并从模型中删除 HABTM 关系。
快乐编码!
This is how bake is designed to work. You're not doing anything wrong except that you have products in the database so it thinks that category_products is HABTM. If you want it to work correctly, you have a few options options:
1- Remove the products table before baking category_products and then add the relationship in to category_products manually.
2- Change the products table to something else during baking, then go back into the models and update the names and relationships to Product.
3- Ignore the error you are getting and remove the HABTM relationship from the model.
Happy coding!