我正在为电子商务项目工作,我有一些问题
每个产品可以属于多个类别
这是我的数据库表:
+-------+-------+
| Product |
+-------+--------
|product_id (PK)|
+---------------+
|name |
+---------------+
+-------+--------+
| Category |
+-------+--------+
|category_id (PK)|
+----------------+
|name |
+----------------+
+-------+--------+
Products_Category|
+-------+--------+
|product_id (PK) |
+----------------+
|category_id (PK)|
+----------------+
这是我的代码ProductsController:
public function add()
{
$product= $this->Products->newEntity();
if ($this->request->is('post')) {
$product= $this->Products->patchEntity($product, $this->request->getData());
if ($this->Products->save($product)) {
$this->Flash->success(__('The product has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The product could not be saved. Please, try again.'));
}
$users = $this->Products->Users->find('list', ['limit' => 200]);
$this->loadModel('Categories');
$categories =$this->Categories->find('all');
$this->set(compact('product', 'users', 'categories'));
}
添加产品时遇到问题
产品在产品表中成功添加
但我还需要将product_id和category_id添加到Products_Category表
我正在使用cakephp框架